본문 바로가기
Java/Java 예습

[Java] 자바의 객체 Part.2 실습_1

by 박채니 2022. 1. 20.

안녕하세요, 코린이의 코딩 학습기 채니입니다.

Java 객체를 예습했던 내용에 대해 실습을 정리한 포스팅입니다.

 

패키지명 :

com.kh.prac1.controller.StrCon

com.kh.prac1.run.Run

 

Run 클래스에서 StrCon 클래스의 메소드 strMethod() 를 실행

strMethod 내용 :
String str1에 리터럴 방식으로 "무궁화 꽃이 피었습니다." 문자열을 생성

String str2에 new 연산자 방식으로 "무궁화 꽃이 피었습니다." 문자열을 생성

String str3에 new 연산자 방식으로 "동해물과 백두산이" 문자열을 생성

 

각각의 String을 hashCode 및 identityHashCode 메소드로 주소값 출력

System.out.println("====== hashCode 비교 ======");   // 3가지 String 변수의 hashCode 출력

 

System.out.println("====== identityHashCode 비교 ======");   // 3가지 String 변수의 identityhasCode 출력

 

출력 답 예시 :

====== hashCode 비교 ======

str1: 313772671

str2 : 313772671

str3 : 282595592

====== identityHashCode 비교 ======

str1 : 118352462

str2 : 1550089733

str3 : 865113938

 

package com.kh.prac1.controller;

public class StrCon {

	public void strMethod() {
		String str1 = "무궁화 꽃이 피었습니다."; 		//리터럴1
		String str2 = new String("무궁화 꽃이 피었습니다.");	//new 연산자 활용1
		String str3 = new String("동해물과 백두산이");		//new 연산자 활용2
		
		System.out.println("====== hashCode 비교 ======"); // String에 한해선 주소값이 아님
		System.out.println("str1 : " + str1.hashCode());
		System.out.println("str2 : " + str2.hashCode());
		System.out.println("str3 : " + str3.hashCode());
		
		System.out.println("====== identityHashCode 비교 ======"); //메모리상의 주소값
		System.out.println("str1 : " + System.identityHashCode(str1));
		System.out.println("str2 : " + System.identityHashCode(str2));
		System.out.println("str3 : " + System.identityHashCode(str3));	
	}	
}

@run
package com.kh.prac1.run;

import com.kh.prac1.controller.StrCon;

public class Run {

	public static void main(String[] args) {
		StrCon sc = new StrCon();
		sc.strMethod();
	}
}

@출력값
====== hashCode 비교 ======
str1 : 313772671
str2 : 313772671
str3 : 282595592
====== identityHashCode 비교 ======
str1 : 793589513
str2 : 1313922862
str3 : 495053715

 

다만, str4에 "무궁화 꽃이 피었습니다"를 리터럴로 입력을 해보았을 때 identityHashCode가 str1가 동일한 것으로 확인된다.

 

package com.kh.prac1.controller;

public class StrCon {

	public void strMethod() {
		String str1 = "무궁화 꽃이 피었습니다."; 		//리터럴1
		String str2 = new String("무궁화 꽃이 피었습니다.");	//new 연산자 활용1
		String str3 = new String("동해물과 백두산이");		//new 연산자 활용2
		String str4 = "무궁화 꽃이 피었습니다.";			//리터럴2
			
		System.out.println("====== hashCode 비교 ======");
		System.out.println("str1 : " + str1.hashCode());
		System.out.println("str2 : " + str2.hashCode());
		System.out.println("str3 : " + str3.hashCode());
		
		
		System.out.println("====== identityHashCode 비교 ======");
		System.out.println("str1 : " + System.identityHashCode(str1));
		System.out.println("str2 : " + System.identityHashCode(str2));
		System.out.println("str3 : " + System.identityHashCode(str3));
		System.out.println("str4 : " + System.identityHashCode(str4));
		
	}
	
}

@출력값
====== hashCode 비교 ======
str1 : 313772671
str2 : 313772671
str3 : 282595592
====== identityHashCode 비교 ======
str1 : 793589513
str2 : 1313922862
str3 : 495053715
str4 : 793589513

new는 매번 다른 메모리 공간을 차지하게끔하지만 리터럴을 사용하면 메모리 공간을 한 곳만 차지!

같은 문자열을 사용하면 같은 메모리 공간에 들어가므로 identityhashcode를 출력해보아도 같은 문자열이기 때문에 같은 값이 나옴