[Tistory] 2024. 7. 9. (화) 슈퍼코딩 부트캠프 Day 32 / 주특기 3주차

원글 페이지 : 바로가기

1. 자바 참조형 생성과 Heap – Java 기본형 : boolean, char, byte, Short, int, long, float, double – Java 참조형 : 기본형을 제외한 나머지 ex. 배열, 클래스, 인터페이스, 열거타입 등 – 참조형 생성 : new로 생성. JVM Heap 메모리 점유 기본형 : int n = 10; -> n이라는 변수에 10 이라는 정수 자체를 지칭 참조형 : 저장된 공간의 주소를 지칭하는 것. 아무 주소도 지칭하지 않으면 null 할당 Java String 생성 : String pool 에 저장됨. String s1 = “Cat”;
String s2 = “Cat”; //리터럴

String s3 = new String(“Cat”); // 객체 생성
s1 == s2; // true
s1 == s3; // false

s1과 s2는 같은 주소값을 가리킴
s3는 heap 영역에 새로운 객체 생성 2. 런타임때 일어나는 일 final 상수, String 등은 Constant pool 에 실행 시 미리 저장 public class JVMTest {
public static void main(String[] args) {
int myint = 5; // stack에 추가
final long mylong = 5L; //실행시 constant pool에 저장-> 선언 시 pool에 저장된 상수 가져와서 stack에 저장
Customer customer = new Customer(“장민철”); //실행시 constant pool에 저장 -> 선언시 stack에 Heap 참조 생성->참조로 constant pool 접근
int[] intArr = new int[5]; //heap 에 저장 후 stack에서 heap 참조
String str1 = “Hello World”; //stack에 저장 후 constant pool 참조 접근
String str2 = new String(“Hello World”); //heap에 새로 객체 생성(constant pool에 있는 Hello World를 참조하여) -> stack에서 heap 참조
customer = null; // customer 주소 null->참조 없어짐(객체는 살아 있음)
myMethod(myint); //stack에 myMethod 프레임 생성
}//종료되면 main 프레임 삭제

static void myMethod(int myint) { //myMethod 프레임에 있는 stack에 myint 변수 저장 -> 기본형이기 때문에 heap에 생성 안함
int myint2 = myint; //myMethod 프레임에 있는 stack에 myint2 저장 -> 기본형이기 때문에 heap에 생성 안함
Customer customer2 = new Customer(“안유진”); //heap에 객체 생성(constant pool에 생성된 객체 참조)->myMethod stack에서 heap 참조 생성
}//종료되면 myMethod 프레임 삭제, myint2 없어짐, 안유진 객체 참조 없어짐(객체는 살아 있음)
}
1. Java 객체 Garbage – 아무도 가리키지 않는 객체 -Garbage Collector가 이 객체들을 수거하여 메모리 정리 System.gc(); // Garbage Collector 호출
//호출 이후에 객체가 사용되는 부분이 있으면 Garbage Collector가 수거하지 않음. 2. Garbage Collector – Mark(수거할 대상 선정) -> Sweep(청소) -> Compaction(빈자리 메꿈) 의 순서로 정리함 – Minor GC : Young Generation(새로운 객체들이 할당되는 영역) -> 자주 청소함 – Major GC : Old Generation (오랫동안 살아남은 객체들이 존재하는 영역) -> 가끔씩 청소함 – Stop-the-World : Major GC 를 실행하는 동안 프로그램이 잠시 멈추는 현상 -Java GC는 자동 수행되기 때문에 직접 호출하지 않아도 된다. 직접 호출은 무거운 작업. Garbage Collector의 순서 (출처 : https://velog.io/@ryukyungwoo1220/16.-Java%EC%9D%98-Garbage-Collector%EB%8A%94-%EC%96%B4%EB%96%BB%EA%B2%8C-%EB%8F%99%EC%9E%91-%ED%95%98%EB%8A%94%EC%A7%80) 3. Java 가상 메소드 다형성의 원리가 적용될 수 있는 멤버 함수(메서드)로써 동적 바인딩으로 처리되는 메서드를 의미 동적 바인딩 수행 시 가상 메서드 테이블을 참조하여 매핑함 https://velog.io/@codren/Java-%EC%98%A4%EB%B2%84%EB%9D%BC%EC%9D%B4%EB%94%A9%EA%B3%BC-%EA%B0%80%EC%83%81-%EB%A9%94%EC%84%9C%EB%93%9C 1. Java Object란? Java.lang 패키지의 기본 클래스 (출처 : https://velog.io/@army246/JAVA-java.lang-%ED%8C%A8%ED%82%A4%EC%A7%80) 2. Java Object 메소드 중요한 메소드 : hashCode(), toString(), clone(), equals() 등 -> 오버라이딩 가능 – toString() : 객체 정보를 문자열로 바꾸어 준다. 기본형 : getClass().getName()+’@’+Integer.toHexString(hashCode())

System.out.println(customer.toString());
// 출력 : Customer@57829d67 – equals() : 두 인스턴스가 같은 객체인지 판단한다. 기본형 : 두 인스턴스의 Heap 주소값을 비교하여 Boolean 값을 리턴

public static void main(String[] args) {
Customer customer1 = new Customer(“ID1”, “민철”);
Customer customer2 = customer1;

Customer customer3 = new Customer(“ID1”, “민철”);

System.out.println(customer1.equals(customer2)); //true
System.out.println(customer3.equals(customer2)); //false
} – hashCode() : 객체의 해시코드 반환 – Clone() : 자신과 같은 객체 복제 1. String 클래스 public static void main(String[] args) {
//CharArray와 String과의 관계

char[] chars = new char[]{‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’};
String str1 = new String(chars); //char -> String
String str2 = String.valueOf(chars); //char -> String

char[] chars1 = str1.toCharArray(); // String -> char
for (char ch : chars1)
System.out.println(ch);
}

} – String 동등 비교 : “==” vs “equals” == : 메모리 주소 비교 equals : 내용 비교 public static void main(String[] args) {
String st1 = “cat”; //constant pool에 생성
String st2 = “cat”; //constant pool에 생성

System.out.println(st1 == st2); //true, 같은 메모리 주소
System.out.println(st1.equals(st2)); //true, 같은 내용

String st3 = new String(“cat”); //Heap 영역에 생성
String st4 = new String(“cat”); //Heap 영역에 생성

System.out.println(st3 == st4); //false, 다른 메모리 주소
System.out.println(st3.equals(st4)); //true, 같은 내용
}

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다