Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Reference

843785Jul 8 2008 — edited Jul 11 2008
Hi, I just started learning Java, and am a bit unfamiliar with how it stores its values. Regarding the code below it apparently outputs "4444". I do not really know why when the "p2.x=p1.y" line is executed, the value of p1.x is changed from 2 to 4. I know it definitely has got to do with the line p1=p2 but I don't really know why. Can anyone please explain? Thanks.

import java.awt.*;

public class Test1 {

public static void main (String [ ] args) {
Point p1 = new Point ( );
p1.x = 1;
p1.y = 2;
Point p2 = new Point ( );
p2.x = 3;
p2.y = 4;
// now the fun begins
p2.x = p1.y;
p1 = p2;
p2.x = p1.y;
System.out.println (p1.x + " " + p1.y
+ " " + p2.x + " " + p2.y);
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 8 2008
Added on Jul 8 2008
8 comments
214 views