Dake Dake - 1 year ago 76
Java Question

Difference between a = b and b = a?

I have to get something straight. Please tell me if I'm correct:

String a;
String b;

a = b means that b takes the value of a?


b = a means that a takes the value of b?

I'm awkwardly confused about this & I'd appreciate an explanation.

Answer Source

Generally, a = b means "a becomes b". However, this is only part of the story when we talk about Objects (as opposed to primitives). After the assignment both variables or fields reference the same object. When object allows changes (which String does not) any changes you perform on a take an effect on b as well, and vice versa.

This can be illustrated with a simple diagram:

Assignment before and after.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download