데이터의 관리를 보다 쉽게하기 위해 만들어진 데이터의 집합
Java8 Orcle 문서 : Java8 Collection
▶자바의 원시타입(Primitive Type) 데이터는 스택에 변수명과 값이 저장되지만, 참조타입(Reference Type)은 실제 객체를 힙 영역에 저장하고 스택에는 객체의 주소만 저장한다.
++ new String()으로 생성할 경우 위와 같이 저장되지만,
String literal(= "value";) 로 생성할 경우 String constanct pool이라는 영역에 존재한다.
==
객체의 스택 값을 비교
-> 원시타입의 경우 스택에 값이 같이 저장되기 때문에 ==
으로도 비교가 가능하지만 new String();
과 같은 참조타입을 비교하면 스택 값은 주소이므로 False가 출력된다.
-> new String();
이 아닌 ==
으로 생성한 String은 ==로 비교했을 때 값이 같으면 True가 출력된다.
equals()
객체의 실제 값을 비교
-> address의 값을 비교하면 10101번지의 값인 "우리집"을 가져와서 비교한다.
hashCode()
자바에서 hashCode()는 주소값을 기준으로 객체 고유의 정수 값이지만, String에서 hashCode()는 자바의 hashCode()와는 다르다.
-> String에서는 값(문자열)이 같으면 같은 hashCode()가 출력된다.
HashMap은 Key 값을 hashCode()로 분류하기 때문에 중복된 Key를 가질 수 없는 것
String one = new String("this");
String two = new String("this");
System.out.println(one == two);
System.out.println(one.equals(two));
System.out.println(one.hashCode());
System.out.println(two.hashCode());
false
true
3559070
3559070
one = "this";
two = "this";
System.out.println(one == two);
System.out.println(one.equals(two));
System.out.println(one.hashCode());
System.out.println(two.hashCode());
true
true
3559070
3559070
String은 불변의 성질을 가지고있기 때문에
one = "this";
처럼 값을 변경하면 새로운 String을 생성한 뒤 대입하는 형태이다
->one = "this";
은new String("this");
와 같다