• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Are Objects passed by value or reference?

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this one of the mock exams . It is quite interesting Please follow the code below.
public class StrBufTest {
public void method1(StringBuffer s1, StringBuffer s2) {
s1.append("There");
s2 = s1; //line 4
}
public static void main(String args[]) {
StringBuffer sb1 = new StringBuffer("Hello");
StringBuffer sb2 = new StringBuffer("Hello");
StrBufTest sbt = new StrBufTest();
sbt.method1(sb1, sb2);
System.out.println("sb1 is " + sb1 + "\nsb2 is " + sb2);
}
}
The Answer to the above question is
sb1 is "Hello There"
sb2 is "Hello".
I can't understand exactly why the value of sb2 is "Hello" and not "Hello There" since at line 4 s2 references the same object as s1. My question is are objects passed by reference. If they are then sb2 should be "Hello There". If they r passed by value then the answer is justified. I am confused because I have read Khalid's book and it mentions that objects are passed by reference.
Please fill me on the same.

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object is passed by reference, but the object reference variable is passed by value. When you pass an object reference variable to a function, you can potentially change the state of the object, but you can't make the passed reference variable refer to another object. An example should help here:
class Apple { boolean yummy; }
...
Apple a1 = new Apple();
tryChanger(a1); //pass copy of a1's reference
System.out.println("Apple is yummy? " + a1.y); //fine!
...
void tryChanger(Apple inA) //See as "Apple inA = a1;"
{
inA.yummy = true; //I can change its state! Affects inA and a1
inA = null; //this only affects inA!
}
...
As for the variables a1 and inA look at it like this...
Apple a1 = new Apple();
Apple inA = a1; //inA now alias of a1
inA.yummy = true; //what you do to inA affects a1
inA = null; //but a1 still has the object
System.out.println("Apple is yummy? " + a1.y);
Hope this helps
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amit Punjwani:
I found this one of the mock exams . It is quite interesting Please follow the code below.
public class StrBufTest {
public void method1(StringBuffer s1, StringBuffer s2) {
s1.append("There");
s2 = s1; //line 4
}
public static void main(String args[]) {
StringBuffer sb1 = new StringBuffer("Hello");
StringBuffer sb2 = new StringBuffer("Hello");
StrBufTest sbt = new StrBufTest();
sbt.method1(sb1, sb2);
System.out.println("sb1 is " + sb1 + "\nsb2 is " + sb2);
}
}
The Answer to the above question is
sb1 is "Hello There"
sb2 is "Hello".
I can't understand exactly why the value of sb2 is "Hello" and not "Hello There" since at line 4 s2 references the same object as s1. My question is are objects passed by reference. If they are then sb2 should be "Hello There". If they r passed by value then the answer is justified. I am confused because I have read Khalid's book and it mentions that objects are passed by reference object sb2 is unchnged.
Please fill me on the same.


I believe function�s arguments in Java are passed by value. So in the case of objects it is copy of the reference to the object. Copies of references to the sb2 and sb2 are put on the stack then the call to your function is made. When you assignee value of s1 to s2 all you do is change the copy which has no effect on the object referenced by it or the reference to the object in the calling code, therefore when the functions returns object referenced by sb2 is unchanged.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amit!
DaB would have cleared your doubt, it not, refer to page 79 in your Kalid Azim Mughal and Rolf Rasmussen's book. In the middle of the page he already explained this topic with reference to previous examples (3.2 & 3.3). The paragraph starts like this "The parameter passing strategy in Java is call-by-value and not call-by-reference, regardless of the type of the parameter. .... ... "
Bye!
Kumaresan.
 
Amit Punjwani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx guys I got it. I was stuck on it for quite sometime.

------------------
 
reply
    Bookmark Topic Watch Topic
  • New Topic