Hi,
when you pass an objectreference as an arg to a method,the changes will be reflected back as far as I know..so here when you pass the two references a&b the output I am getting as onemore followed by two....in the method swap b is refering to a so b value is onemore in the swap method but in the main method it is giving me the output as onemore followed by two....can anyone please explain ..I will be so thankful to you..
public class
Test {
public static void main(
String args[]) {
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Test.swap(a,b);
System.out.println("a is "+ a +"\nb is " + b);
}
static void swap (StringBuffer a, StringBuffer b) {
a.append(" more");
b=a;
}
}
What will be the output?
Answer:
a. a is One
b is Two
b. a is One
b is One
c. a is One more
b is One more
d. a is One more
b is Two
e. a is One more
b is Two more
Regards
Krishna