Java에서 String 비교시 equals를 사용하는 이유

package myproj;

public class ClassNameAddrTest {

 public static void main(String[] args) {

  String str1 = "aa";
  String str2 = "aa";
  String str3 = new String("aa");
  String str4 = "a" + "a";


  System.out.println(getAddr(str1));
  System.out.println(getAddr(str2));
  System.out.println(getAddr(str3));
  System.out.println(getAddr(str4));
  System.out.println((str1 == str2));
  System.out.println((str1 == str3));
  System.out.println((str1 == str4));
  System.out.println(str1.equals(str3));
  System.out.println(str1.equals(str4));
 }
 

//reference 출력
 public static String getAddr(Object o){
  return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
 }
}

Console 결과


 

'프로그래밍 > Java' 카테고리의 다른 글

reflect Method invoke 활용  (0) 2014.04.29
Posted by 란수
,