First go through the code -->
class MWC106 {
static void m1(
String s) {
s = s.trim();
s = s.concat("D");
}
public static void main(String[] s) {
String s1 = "A", s2 = " B ", s3 = "C";
m1(s2);
System.out.print(s1 + s2 + s3);
}}
In this code snippet, the output should be ABDC according to me. But the real output is ABC.
My explaination goes like this. in m1() method we are passing a String reference. so in the defination of method, it will get the copy of reference BUT both pointing to the same String Object. so we are modifying those object values in the method, it means the value objects are modified, when we print it in the main method using the rerence s2, it should get modified through method call.
So it should be ABDC. But the answer I know , I am wrong somewhere. please clarify me this.
Thanx & Regards,
Nandish