develog

String 비교 본문

Dev/Java

String 비교

냐옴 2013. 7. 2. 13:28

String str1 = "ABC";

String str2 = new String("ABC");

String str3 = "ABC";


System.out.println("str1 : " + str1);

System.out.println("str2 : " + str2);

System.out.println("str3 : " + str3);

System.out.println();


System.out.println("str1.toString() : " + str1.toString());

System.out.println("str2.toString() : " + str2.toString());

System.out.println("str3.toString() : " + str3.toString());

System.out.println();


System.out.println("str1.hashCode() : " + str1.hashCode());

System.out.println("str2.hashCode() : " + str2.hashCode());

System.out.println("str3.hashCode() : " + str3.hashCode());

System.out.println();


System.out.println("(str1 == str2) : " + (str1 == str2));

System.out.println("(str1 == str3) : " + (str1 == str3));

System.out.println("(str2 == str3) : " + (str2 == str3));

System.out.println();


System.out.println("str1.equals(str2) : " + str1.equals(str2));

System.out.println("str1.equals(str3) : " + str1.equals(str3));

System.out.println("str2.equals(str3) : " + str2.equals(str3));

System.out.println();


System.out.println("Long.toHexString(System.identityHashCode(str1)) : " + Long.toHexString(System.identityHashCode(str1)));

System.out.println("Long.toHexString(System.identityHashCode(str2)) : " + Long.toHexString(System.identityHashCode(str2)));

System.out.println("Long.toHexString(System.identityHashCode(str3)) : " + Long.toHexString(System.identityHashCode(str3)));

System.out.println();



// console

--------------------------------------------

str1 : ABC

str2 : ABC

str3 : ABC


str1.toString() : ABC

str2.toString() : ABC

str3.toString() : ABC


str1.hashCode() : 64578

str2.hashCode() : 64578

str3.hashCode() : 64578


(str1 == str2) : false

(str1 == str3) : true

(str2 == str3) : false


str1.equals(str2) : true

str1.equals(str3) : true

str2.equals(str3) : true


Long.toHexString(System.identityHashCode(str1)) : 1c78e57

Long.toHexString(System.identityHashCode(str2)) : 5224ee

Long.toHexString(System.identityHashCode(str3)) : 1c78e57



'Dev > Java' 카테고리의 다른 글

[java] 배열내 중복 체크  (0) 2013.07.02
generate file name  (0) 2013.07.02
Object.toString(), System.identityHashCode()  (0) 2013.07.02
진수 변환  (0) 2013.07.02
log4sql  (0) 2013.06.28
Comments