ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] 문자열 비교 - equals() 메서드 vs == 연산자
    카테고리 없음 2024. 7. 14. 17:31
    String 선언 차이 (literal vs new)
    • new로 선언하면 java heap memory에 개별 객체가 생성됨
    String strA = new String("Hello World");
    • literal로 선언하면 String Constant Pool에 생성된 객체를 참조함
    String strB = "Hello World";

     

     

    equals() 메소드
    • 두 문자열의 내용을 비교하는 메서드
    String strA = "Hello World";
    String strB = "Hello World";
    String strC = new String("Hello World");
    
    System.out.println(strA.equals(strB)); // true
    System.out.println(strA.equals(strC)); // true

     

    == 연산자
    • 두 문자열의 주소값을 비교하는 연산자
    String strA = "Hello World";
    String strB = "Hello World";
    String strC = new String("Hello World");
    
    System.out.println(strA == strB); // true
    System.out.println(strA == strC)); // false

     

Designed by Tistory.