inblog logo
|
sson17
    자바

    [JAVA]62.Equals()

    손영민's avatar
    손영민
    Mar 09, 2025
    [JAVA]62.Equals()
    Contents
    객체 비교의 두 가지 방식
    ❗
    문자열은 equals()로 비교해야 합니다!

    객체 비교의 두 가지 방식

    Java에서 객체를 비교할 때는 두 가지 방식이 있습니다:
    1. == 연산자: 참조(메모리 주소) 비교
    1. equals() 메서드: 객체의 내용(값) 비교
     
    package ex17; public class Ha03 { public static void main(String[] args) { // 가니까 값이 있다. int n1 = 1; int n2 = 1; System.out.println(n1 == n2); // 가니까 주소가 있다. 근데 참조주소가 같다. String s1 = "A"; String s2 = "A"; System.out.println(s1 == s2); s2 = s2 + "B"; System.out.println(s1 == s2); //최종목적지를 검사해보자. (값을) - equlse ((1)== 통과못하면 -> (2)최종 값) String s3 = new String("A"); String s4 = new String("A"); System.out.println(s3.hashCode()); System.out.println(s4.hashCode()); System.out.println(s3 == s4); //같더라도 메모리주소가 다르면 false가 뜬다 System.out.println(s3.equals(s4)); //메모리주소와 문자열 비교하는데 문자열만 같으면 true 가 뜬다. } }
    Share article
    Contents
    객체 비교의 두 가지 방식

    sson17

    RSS·Powered by Inblog