<CS 지식> 자바 문법 (Primitive Type, Reference Type, Auto boxing, Auto unboxing)

Google 아니고 Joogle·2022년 7월 27일
0

CS 지식

목록 보기
13/22

1. Primitive Type

  • 총 8가지의 Primitive Type 존재 (boolean, byte, short, int, long, float, double, char)
  • stack memory에 저장
  • 비객체 타입이고 null값을 저장할 수 없음

2. Reference Type

  • Primitive Type을 제외한 타입들이 모두 Reference Type
  • Class type, Interface Type, Array Type, Enum Type (열거 타입)
  • java.lang.Object 클래스를 상속하는 모든 클래스
  • new로 인하여 생성하는 것들은 메모리 영역인 Heap 영역에 생성 -> Gabage Collector가 돌면서 메모리를 해제함
int[] array ={1,2};
int[] array2 =array;

array2[2]=3;
======>array-> {1,3}, array2->{1,3} 같은 결과 출력
  • 문법상으로는 에러가 없지만 실행시켰을 때 에러가 나는 Runtime Error 발생 (e.g. 객체나 배열을 Null 값으로 받으면 NullPointException이 발생)
  • Heap 메모리에 생성된 인스턴스는 메소드나 각종 인터페이스에 접근하기 위해 JVM의 Stack 영역에 존재하는 Frame에 일종의 포인터인 참조값을 가지고 있어 이를 통해 인스턴스를 핸드링 함

3. Auto Boxing & Auto Unboxing

Boxing & Unboxing

  • boxking : 기본 타입 데이터에 대응하는 Wrapper class로 만드는 동작 (값->참조)
  • Unboxing : Wrapper Class에서 기본 타입으로 변환
int i=10;
Integer num=new Integer (i); //boxing
Integer num=new Integer (10);
int i=num.intValue(); //unboxing
  • 값형이 참조형으로 바뀔 때, 스택에 있는 값을 복사하여 힙에 할당하게 되고 언박싱시 다시 스택에 가져오는 작업을 하면서 힙에 가비지가 쌓임 -> 성능적 이슈가 생김 -> Generic이 이 문제 해결

Wrapper Class

  • Integer, Long, Float, Double, Boolean

reference

https://gyoogle.dev/blog/computer-language/Java/Primitive%20type%20&%20Reference%20type.html
https://yeko90.tistory.com/entry/c-%EB%B0%95%EC%8B%B1-%EC%96%B8%EB%B0%95%EC%8B%B1-%EA%B0%9C%EB%85%90

profile
Backend 개발자 지망생

0개의 댓글