Skip to content

문자열 비교는 equals() #8

@SeoMiYoung

Description

@SeoMiYoung

코테에서 Java 문자열 비교는 진짜 자주 함정 나와요.

❌ 절대 하면 안 되는 것

if (a == b) { ... }

👉 이건 문자열 내용 비교가 아니라 참조(주소) 비교라서 코테에서 터집니다.

✅ 정답: equals()

if (a.equals(b)) {
    // 두 문자열 내용이 같을 때
}

예시

String a = new String("hello");
String b = new String("hello");

System.out.println(a == b);      // ❌ false (==는 내용이 아니라 주소 비교다)
System.out.println(a.equals(b)); // ✅ true

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions