aspose file tools
The moose likes Beginning Java and the fly likes String object created twice with the same reference variable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "String object created twice with the same reference variable" Watch "String object created twice with the same reference variable" New topic
Author

String object created twice with the same reference variable

mvPrasad Regula
Ranch Hand

Joined: Sep 14, 2009
Posts: 42
class Temp {
public static void main(String[] args)
{
String a,b,c;
c= new String("mouse");
a=new String("Car");
b=a;
a= new String("Dog");
c=b;
System.out.println(c);
}
}

Here I am getting the output as Car. The same reference variable a has been instantiated twice. So the reference variable a should now point to the newly created object "Dog". As c is refering to b which is refering to a, the output is supposed to be Dog.


Prasad Regula
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17243
    
    1

Moving this to Beginning Java, this is a forum for Spring Framework.

Good Luck

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ulrika Tingle
Ranch Hand

Joined: Nov 24, 2009
Posts: 92
mvPrasad Regula wrote:As c is refering to b which is refering to a, the output is supposed to be Dog.


Java doesn't have assignment by reference. It has assignment by value (of references). So when you do,

b = a;

this does not mean that b becomes an alias for a and that both a and b are now two symbols for the same variable. That would be a by reference assignment and Java doesn't have that.

Instead what happens is that the value held by a (a reference) is replacing the value currently held by b. Afterwards the two variables a and b hold the same value (the same reference pointing at an object). This is an assignment by value of a reference and that's what Java does.

Java doesn't do anything by reference. It does everything by value, both assignments and parameter passing. The value assigned or passed can be a reference though.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12953
    
    3

Look at what's happening line by line:

  • c refers to a String object containing "mouse"
  • a refers to a String object containing "Car"
  • b refers to the same String object that a refers to (the one containing "Car")
  • a is now changed to refer to a String object containing "Dog" (note that nothing has changed with b!)
  • c is now changed to refer to the same String object that b refers to (the one containing "Car")

  • So in the end, c refers to the "Car" String object.

    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
    Scala Notes - My blog about Scala
    Ulrika Tingle
    Ranch Hand

    Joined: Nov 24, 2009
    Posts: 92
    Jesper Young wrote:
    (note that nothing has changed with b!)


    You say so but what's the reason?

    Hint: Look at my reply.
     
    I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
     
    subject: String object created twice with the same reference variable
     
    Similar Threads
    Another examLab casting question
    Overriding and Co-variant returns doubt
    Difference bet == and (.equals)
    objects and refrerences : a basic question
    Can I caste Object[] to String[]?