• 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

Object References - why are Strings different?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have played with the following code, partially derived from Mughal p.49, and I still can't explain to myself the rationale for the result.

public class Tester {
int i = 1;
public static void main (String[] args) {
String a,b,c;
c = new String("mouse");
a = new String("cat");
b = a;
a = new String("dog");
c = b;
//c = "cat", but I am expecting "dog" since c references b and b references a and a
//reassigned the string it refered to to be "dog".
System.out.println ("c = " + c);
// --------------------------------------------------------
Tester tc = new Tester();
Tester ta = new Tester();
Tester tb = new Tester();
tb = ta; //assign b to a, just like above
ta.i = 2; //change a, just like above
tc = tb; //assign c to b, just like above
//Expecting tc.i to equal 2, since it points to tb which points to ta which
//reassigned i to equal 2. This worked as I expected.
System.out.println("tc.i = " + tc.i);
}
}

Having played with this for over an hour and examining Mughal's explanation on p. 625, in addition to RHI and The Java Tutorial (Sun), I am absolutely baffled. I love this language!


[This message has been edited by Michael Morett (edited November 04, 2001).]
[This message has been edited by Michael Morett (edited November 04, 2001).]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1: c points to a new String instance whose value is "mouse"
2: a points to a new String instance whose value is "cat"
3: b points to the String instance pointed by a ("cat"), thus b points to a String instance whose value is "cat"
4: a points to a new String instance whose value is "dog"
5: c points to the String instance pointed by b ("cat"), thus c points to a String instance whose value is "cat"
when making an assignment like c=b it does not mean that c is now pointing to b but c is now pointing to the value pointed by b. If b points to somewhere else c is not affected by that change. It would be a true nightmare if it was the case. Forget about C++ !!! There is no "explicit" pointers in Java (Thank God) !
Same explanation for the other example
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Michael Morett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Valentin,
I am still confused by line 3 (b = a).
I am pouring through JLS, JPL, and other material and I still fail to understand why "b" does not reflect the latest reference "a" makes to "dog".
Here's what I am thinking...
"a" is a reference (to a literal)
"b" is a reference (to a reference)
Hence, "b" contains a reference to "a" which contains the value of "dog". But this ain't so.
The only thing I can think of is that the principle of pass by reference (JPL p.56) is in effect here.
Am I on the right track?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michael :
The main ideal likes this:
ex: String a, b;
a = new String("cat");//a is a ref to String object containing value cat
b = a;//both b and a point to the same String object
// a ref -->String Object <-- b ref .Note b and a have the same value <br /> but You think b ref --> a ref --> String object .Note b points to a, a points to String object(wrong)
Now, a = new String("dog");
b still point to old object b--> old String object("cat")
but a points to new String object ("dog")

Hope this help
 
Michael Morett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lu,
That's it! Now it all makes sense. Your drawing of...

a ref -->String Object <-- b ref <br /> and<br /> b ref --> a ref --> String object
...is what made it click.
I owe you one.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have some confuse.
/*this is your code*/
c = new String("mouse");//1
a = new String("cat");//2
b = a;//3
a = new String("dog");//4
c = b;//5
/////////////////////
at line 1 : c reference to "mouse" object
at lne 2 : a --- "cat" onject
3 : b --- "cat" object
4 : a --- "dog" object but b still --- "cat"
5 : c = b so c --- "cat"

 
I love a good mentalist. And so does this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic