Wednesday 15 January 2014

java - How come the object "person1" and the object "person2" are the same? -



java - How come the object "person1" and the object "person2" are the same? -

in program, whenever alter person1 or person 3, become contain same values. tried going through each step in pseudo code, lost on reasoning behind these 2 objects beingness equal. help explain these steps? love understand. give thanks time.

public class references1 { public static void main (string[] args) { person person1 = new person ("rachel", 6); person person2 = new person ("elly", 4); person person3 = new person ("sarah", 19); system.out.println ("\nthe 3 original people..."); system.out.println (person1 + ", " + person2 + ", " + person3); // reassign people person1 = person2; person2 = person3; person3 = person1; system.out.println("\nthe 3 people reassigned..."); system.out.println (person1 + ", " + person2 + ", " + person3); system.out.println(); system.out.println ("changing sec name bozo..."); person2.changename ("bozo"); system.out.println (person1 + ", " + person2 + ", " + person3); system.out.println(); system.out.println ("changing 3rd name clarabelle..."); person3.changename ("clarabelle"); system.out.println (person1 + ", " + person2 + ", " + person3); system.out.println(); system.out.println ("changing first name harpo..."); person1.changename("harpo"); system.out.println (person1 + ", " + person2 + ", " + person3); } }

these lines causing problem:

// reassign people person1 = person2; person2 = person3; person3 = person1;

if variables had next starting values:

person1 = "a"; person2 = "b"; person3 = "c";

... , ran through code:

person1 = person2 -> person1 set "b" (the value "a" discarded) person2 = person3 -> person2 set "c" person3 = person1 -> person3 set value of person1 "b"

so variable person1 , person3 set same object.

java object pseudocode

No comments:

Post a Comment