简体   繁体   中英

Don't understand references

I just don't understand why t.getName() is Keen if t is assigned to u.

public class StringProgram{
  public static void main(String[] args) {


    Person t = new Person("Gene");
    Person u = t;
    u.setName("Keen");

    System.out.println(t.getName());//Keen
    System.out.println(t.equals(u));//true


  }
}

Person class

public class Person{
    private String name;

    public Person(String d){
    name=d;
    }
    public void setName(String a){
      name=a;
    }

    public String getName(){
      return name;
    }

     @Override
    public boolean equals(Object o) {
        if (!(o instanceof Person)) {
            return false;
        }

        return ((Person) o).name.equals(this.name);
    }
}

I am somewhat new too, but I think this is why it will print "Keen". This will take a foray into java object usage

First thing first, Person t=new Person("Gene") creates a person object that has the name variable, "Gene". Then, it uses a reference, in your case t , to refer to the object. So t will always refer to that Person object.

Then, with your next line, Person u=t , you are creating a new reference that actually refers to the same Person object with "Gene" as the original t reference. Using the = sign made them do the same thing, refer to "Gene", just like equality with math operations in Java makes the left hand equal to the right hand.

Hence, whether you refer to the "Gene" object with u or t , you are referring to the same thing. Hence, in either u.setName("Keen") or t.setName("Keen") , you are reaching for the same object somewhere in memory and doing something to/with it, in this case changing the name variable to "Keen".

It is exactly like user4932934 mentioned in their comment above. Note that line:

Person t = new Person("Gene");

uses "new" keyword - it tells you that new object is created. Line:

Person u = t;

simply assigns "t" to "u". Now they refer to the same object in memory, no new instance of Person was created.

Person u = t; makes u refer to the exact same object as t . The same memory location, if that helps. So changing the contents of t changes the contents of u too. Because they're the same object.

I will share with you metaphor regarding the references i once read.

Imagine that your Person object is balloon. Your reference t is a string (rope, not java String) attached to balloon.

So, when you create another reference u which is same like t , you created another string(rope) attached to the one and same balloon (Person).

If you change property of your balloon, eg colour, both u and t will see this change, since you only have one balloon attached to both u and t.

So when you assign reference to another reference, you are not creating new object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM