Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes reference variable doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "reference variable doubt" Watch "reference variable doubt" New topic
Author

reference variable doubt

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
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


Thanks and Regards, Amit Taneja
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18552
    
  40

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 ...


Remember that strings are constant -- they can't be changed. All changes are done by changing the reference. So all the method did was change the reference to point to something else -- not change the original object.

Of course, the answer would have be different if it was a stringbufffer object.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
i think i got it...

when ever the referense is past of string constant to other reference variaible both refer to same string constant
and if 2nd reference variable changes the string ..then another string is created and refer to that object...

for eg.

String a= "first object";
String b=a; // now both referencing to same object...
b=b.concat(" hi"); // now another object created having value "first
//object hi"


is i m rt sir ??

thanx for your reply...

one more thing....

can you tell me how to master the topic "threads"

i wanted to make webserver.....how much advanced thread programming is needed...

to be honest ... i have not made any much thread program so bit afraid of it...

just give me some inspiration to make me thread master....
which resourse to read..other then ur book
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reference variable doubt
 
Similar Threads
nested class Danchisholm ques.
DAN's Qn doubt
pass by value & ref
nested class Danchisholm ques.
string doubt