JAVA reference variable

MangoMelon·2021년 4월 29일
0

Default types in java use duplication for = operation.

int a = 1;
int b = a;
b = 3;
---- result ----
a : 1 , b : 3

Like C lang, int type object has own storage for save number. So change of b doesn't affect to a. = operation only changed number of b, not address of b.

Otherwise, all class names contain only address of object, not class properties. So name of class is called reference variable. Consequently a and b in example below direct equal memory address and work like equal variable.

//Example Node class defined
//Node class has property int num;
Node a = new Node();
a.setNum(1);
b = a;
b.setNum(3);
---- result ----
a.num : 3, b.num : 3

Let's see example below.

String a = "Hello";
String b = a;
b = "World";
---- result ----
a : "Hello", b : "World"

However b duplicated address of a, the result is not {a : "World", b : "World"}. Even b have an same address with a, address of b will be replaced when new object address is injected. In this case, b got a new String "World".

profile
I'm the BEST

0개의 댓글