hi in one of the DAN question
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);
}}
What is the result of attempting to compile and run the program?
a. Prints: ABC
b. Prints: A B C
c. Prints: ABCD
d. Prints: ABDC
e. Prints: A B CD
f. Prints: A B DC
g. Compile-time error
h. Run-time error
i. None of the above
then answer is "b"
with explaination... as follow
The String instance referenced by s2 is passed to the m1 method by passing the value of the reference. The reference value used in method m1 is a local copy of the reference. If the local copy used in method m1 is changed, then the original reference variable in the main method remains unchanged
but i don't understand why this answer is correct ...because when ever the reference variable to method is passed ...another reference variable ( in method called) now contains the address of the object in memeory...
so change through reference variable even in method will change the actual object ...
this is what i have read ..but its contradictory to the above answer...
pls expain the rt answer and why it right ??
thanx