• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

StringBuffers??

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What should be the output of the foll piece of code?
public class test
{
public static void main(String args[])
{
StringBuffer a = new StringBuffer("Hello");
StringBuffer b = new StringBuffer("Hello");
testtestObj = new test();
testObj.convert(a,b);
System.out.println(a);
System.out.println(b);
}

public void convert(StringBuffer x,StringBuffer y)
{
x.append("There");
y = x;
}
}
a)Hello ,Hello
b)Hello there,Hello there
c)Hello there,Hello
d)Compiler Error
Me thinks it should be b,but when I executed the code,it displayed c.
Any idea why???
Gayatri
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only changed the local reference of Y and not of B. When you pass objects to methods, you are passing a reference to the object. If you change data memebers of the reference then both the call in the method and the call to outside the method will reflect the change.
Someone made this analogy earlier and it kind of stuck with me: Think of a refernece as a sign pointing to a city. When you pass the objects to a method, you are creating new signs, so in your example, you just created 2 new signs. In A and X, you changed the city, that is you changed some of the data in the StringBuffer. Then you said, now I want Sign Y to point to the same city as A and X, which is fine, but you didn't change sign B. So when you printed out B, you got just "Hello."
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer should be C and infact there should not be any space between Hello and There .
Though you are passing copy of the objects a and b to the convert method - only x is getting operated and hence the pool value for a is modified to HelloThere.
Since y is just a reference assignment inside the method and the scope is only within the method convert and hence the result.
Hope this helps,
Anna S Iyer.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gayathri,
In Java,values are passed by value.
Inside the method , u can change the state of the object
but not the reference itself.
So C is correct.


public void convert(StringBuffer x,StringBuffer y)
{
x.append("There");
y = x;
}


Here X is an another reference to object a.
So any modification will be reflected,as u can access the same object pointed by a.While y=x will not be reflected when the method comes out...so when u print 'b' (which is pointing to "hello") u got the msg thus..

Before method call:

+------------+
| | <----------------- a
| Hello |
+------------+
+------------+
| | <----------------- b
| Hello |
+------------+

Inside the method:
+------------+
| | <----------------- a
| Hello | <----------------- x
+------------+
+------------+
| | <----------------- b
| Hello | <----------------- y
+------------+
Outside the method:
+------------+
| | <----------------- b
| Hello |
+------------+

b still points to "hello"
Hence the result



Hope this helps..
Jeban.
[This message has been edited by Jonathan Jeban (edited October 16, 2000).]
 
Gayatri Deshpande
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,Bill and Anna.
I got the concept now.
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic