I have a bit of an odd question regarding how primitive types work in Java. When using Objects when you assign an ObjectA to be ObjectB be like such
Rectangle ObjectB = new Rectangle();
ObjectA = ObjectB;
Any calls to ObjectA refer now to ObjectB's memory location. However when using integers or other primitive types this is not the case. For example
int x = 3;
int y = x;
int x = 5;
return y;
y will return 3, the value of x when y was initialized.
The question I have is why does assignment for objects create references in memory, whereas primitives make a copy of the values of each other? Other than the fact that this is useful, how is this implemented in Java?
I'd appreciate anyone who can provide me a greater understanding of how assignment works between primitive types and objects.