gwooden_코린이

java_API equals_221130(8일차) 본문

java/8일차

java_API equals_221130(8일차)

gwooden22 2022. 11. 30. 15:51
728x90

기본 자바 API

equals, hashCode

package 자바API;

public class Sample01 {

	public static void main(String[] args) {
		
			// equals 메서드
		
		//String str1 = "abc";랑 같은 코드
		String str1 = new String("abc"); //새롭게 객체를 생성
		String str2 = new String("abc");
		
		System.out.println("str1 : " +str1.hashCode());
		System.out.println("str2 : " +str1.hashCode());
		System.out.println("str1 : " + System.identityHashCode(str1));
		System.out.println("str2 : " + System.identityHashCode(str2));
		
		if(str1 == str2) {
			System.out.println("같다");
		} else {
			System.out.println("다르다");
		}
		
		
		
		
		
		
		
		if(str1.equals(str2)) {
			System.out.println("같다");
		} else {
			System.out.println("다르다");
		}

	}

}

 


 

728x90
Comments