*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ajith, Only you can explain clearly! Thank you 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 "Ajith, Only you can explain clearly! Thank you" Watch "Ajith, Only you can explain clearly! Thank you" New topic
Author

Ajith, Only you can explain clearly! Thank you

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I thought the answer would be HELLOTHERE,HELLOTHERE but sb2 is still HELLOO.
can anyone explain this.

answer is HELLOTHERE,HELLOO
Thanks in advance.

[I added UBB CODE tags to your source code to make it more readable. Please try to use them in the future - Ajith]
[This message has been edited by Ajith Kallambella (edited August 31, 2000).]
Harry Chawla
Ranch Hand

Joined: Jun 03, 2000
Posts: 97
U can search on the ranch for HELLOTHERE as this question has been discussed many times.
I searched for HELLOTHERE and found that you are putting two posts for the same question.
We'd appreciate if u can try to look around rather than floating two more copies of the same post...
Thanks
kishen uchil
Greenhorn

Joined: Aug 21, 2000
Posts: 14
what actually happens is that when u pass the arguments sb1 &sb2 to the method method1() "t.method1(sb1,sb2)" a copy of the reference is passed to the method and not the actual reference.actually in this line the references are copied and passed to the method.now inside the method what u are doing is tthat u are actually modifying the object via the reference so s1.append modifies the object and hence sb1 also changes
and the changes are visible to the caller method (the main method in this case).however the variable sb2 is not modified in any such way
so there is no effect on the object.
please insert these two lines after "s2=s1;"(i.e after line 4)
System.out.println("s1 is"+s1);
System.out.println("s2 is"+s2);
it will make things clear.
consider the following examples:
1.

here on line line two the callers variable is copied and passed into bumper() and incremented by 15.since the original xx is untouched ,line 3 will show that xx is 12345.

2.

here in the changer method you the object is modified via the reference
as distinguished from modifying the method argument(the reference)so the changes will be visible to the caller method and System.out.println(tf.getLabel);will print "hi there" and not "hi".

[I added UBB CODE tags to your source code to make it more readable. Please try to use them in the future - Ajith]
[This message has been edited by Ajith Kallambella (edited August 31, 2000).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Ajith, Only you can explain clearly! Thank you
 
Similar Threads
Passing arguments to method
StringBuffer
String Buffer
StringBuffer
arguments to method