Question discussion

What will be the output?public class Test{ public static void main(String[] args){ String value = "abc"; changeValue(value); System.out.pri…

299913Medium 1 reply 754 views
0
What will be the output?public class Test{ public static void main(String[] args){ String value = "abc"; changeValue(value); System.out.println(value); } public static void changeValue(String a){ a = "xyz"; }}
  • Aabc
  • Bxyz
  • CCompilation fails
  • DCompilation clean but no output
  • ENone of these
Correct answer: A

Discussion 1

0
CCommunity
abc

Java pass reference as value. passing the object reference, and not the actual object itself. Simply reassigning to the parameter used to pass the value into the method will do nothing, because the parameter is essentially a local variable.