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 출력
a
에 3
이 보관되고, b
에는 a
의 값인 3
이 복사돼서 보관
그 다음에 a = 4
를 하면 a
만 바뀌고 b
는 그대로 3
마찬가지로 b = 7
을 하면 b
만 바뀌고 a
는 그대로 4
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 s1 = "hello";
String s2 = "hello";
String ss1 = new String("hello");
String ss2 = new String("hello");
String s1 = "hello"
라고 선언한 s1
과 s2
는 ==
을 통해 물어보면 true
라 출력
String ss1 = new String("hello")
라고 선언한 ss1
과 ss2
는 ==
을 통해 물어보면 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