It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about reference" Watch "about reference" New topic
Author

about reference

wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56
7. Select the correct answer for the code fragment given below?
public class TestBuffer {
public void myBuf( StringBuffer s, StringBuffer s1) {
s.append(" how are you") ;
s = s1;
}
public static void main ( String args[] ) {
TestBuffer tb = new TestBuffer();
StringBuffer s = new StringBuffer("Hello");
StringBuffer s1 = new StringBuffer("doing");
tb.myBuf(s, s1);
System.out.print(s);
}
}
A) Prints Hello how are you
B) Prints Hello
C) Prints Hello how are you doing
D) Compile time error
Answer 7:
A) Prints Hello how are you
Explanation:
Assigning or interchanging the object references does not change the values, but if you change the values through object references , changes the values .
-------
i m still a bit confused about the explanation,would you give me futher explanation using example? how can i change the valuses through object references?
amit mawkin
Ranch Hand

Joined: Oct 31, 2001
Posts: 73
Hi,
try to read some topics of arguements passing to methods in java
genearlly under pass by value and pass by reference,
generally a copy is passed to methods .
so actual value is not altered
------------------
coffee drinker
-----------------------
Sun Certified Programmer for Java 2 Platform
[This message has been edited by amit mawkin (edited December 18, 2001).]


regards, Amit
coffee drinker and Sun Certified Programmer for Java 2 Platform
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
wei,
if you haven't read this http://www.javaranch.com/campfire/StoryPassBy.jsp
object references does not change the values:
Since they are copies of the real references thats passing to a method they will stay the same after the method. Think of it this way, You have a new references not the same one that you want to pass. This it is a copy and a totally new reference. Why would it affect the original reference? It is a new reference. It has no relation with the original one. For example, when u passing int, long, double, char, short.. etc etc.

but if you
change the values through object references:

Since a Object references points to the real object so if you change the values through that references the changes stay after the method.
reference_A --------> StringBuffer_object.
In the method, changing just the reference wouldn't change anything after the method, like what i have said above.
Since the reference got copied not the actually Object!!!
changes will take affect. For example.
in main(...) method.
reference_original ---------> My_Object
in some_method(.....) which called from main() which passing My_Object.
Reference_copy_of_original --------> My_Object
The reference got copied but pointing to the same Object.
So, if you use the copied reference to change a memebers within My_Object you are change the actually Object thats in main(..)
since it is the same Object. The point is ......
Object Reference passed to methods are copies!
What they are pointing is the same acutally Object!

Hope this help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about reference
 
Similar Threads
Question from Javacaps mock
Queries
about reference
A String Buffer question,help!
I thought it should be "Hello" instead of "Hello how are you"