Primitive Type

int a = 3;
int b = a;

System.out.println(a);  // 3 출력
System.out.println(b);  // 3 출력

a = 4;
System.out.println(a);  // 4 출력
System.out.println(b);  // 3 출력

b = 7;
System.out.println(a);  // 4 출력
System.out.println(b);  // 7 출력

a3이 보관되고, b에는 a의 값인 3복사돼서 보관

그 다음에 a = 4를 하면 a만 바뀌고 b는 그대로 3

마찬가지로 b = 7을 하면 b만 바뀌고 a는 그대로 4

Reference Type

Person p1, p2;
p1 = new Person("김신의", 28);

p2 = p1;
p2.setName("문종모");

System.out.println(p1.getName()); // 문종모
System.out.println(p2.getName()); // 문종모

int[] a = new int[3];
int[] b = a;

a[0] = 1;
b[0] = 2;

System.out.println(a[0]); // 2
System.out.println(b[0]); // 2

String

String s1 = "hello";
String s2 = "hello";

String ss1 = new String("hello");
String ss2 = new String("hello");

String s1 = "hello"라고 선언한 s1s2==을 통해 물어보면 true라 출력

String ss1 = new String("hello")라고 선언한 ss1ss2==을 통해 물어보면 false

String s1 = "hello";
String s2 = "hello";

String ss1 = new String("hello");
String ss2 = new String("hello");

System.out.println(s1 == s2); // true
System.out.println(s1.equals(s2)); // true
System.out.println(ss1 == ss2); // false
System.out.println(ss1.equals(ss2)); // true