Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a = b and b = a?

Tags:

java

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?

And

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

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

like image 681
Dake Avatar asked Aug 19 '16 16:08

Dake


1 Answers

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.

like image 121
Sergey Kalinichenko Avatar answered Oct 06 '22 00:10

Sergey Kalinichenko