[Java] 자료형

김선형·2025년 9월 6일

Java

목록 보기
2/27

기본형 (Primitive)와 참조형 (Reference)로 구분된다.

기본형

자료형크기 (byte)기본값설명예시
byte10정수형, -128~127byte b = 100;
short20정수형, -32,768~32,767short s = 30000;
int40기본 정수형int i = 100000;
long80큰 범위의 정수형, 접미사 L 필요long l = 10000000000L;
float40.0f실수형, 접미사 f 필요float f = 3.14f
double80.0기본 실수형double d = 3.141592;
char2'\u0000'문자형, 유니코드 사용char c = 'A';
boolean-falsetrue 또는 fasleboolean flag = true;

참조형

자료형크기 (byte)기본값설명예시
String-null문자열 객체 (클래스)String msg = "Hello";
배열 ([])-null같은 타입의 집합int[] arr = {1, 2, 3};
사용자 정의 클래스-null객체 생성 필요Student s = new Student();

✏️ 기본형 vs 래퍼 (Wrapper) 클래스

기본형

  • 가장 기본적인 데이터 타입으로, 값 자체를 저장한다.
  • byte, short, int, long, float, double, char, boolean
  • 참조가 아닌 값 자체를 저장하며, 메모리 효율적이고 연산 속도가 빠르다.
  • 객체 메서드를 사용할 수 없다.

래퍼 클래스

  • 기본형을 객체 형태로 감싸는 클래스
  • Byte, Short, Integer, Long, Float, Double, Character, Boolean
  • 객체이므로 메서드를 사용할 수 있다.
  • 컬렉션에 저장할 수 있으며, null을 저장할 수 있다.
  • 기본형을 래퍼 클래스로 변환하는 boxing과 그 반대인 unboxing에 대해, auto-boxing과 auto-unboxing을 지원한다.

✏️ 키워드 (Keyword)?
Java가 미리 정의해 놓은 예약어로, 변수 이름, 메서드 이름 등으로 사용할 수 없다.

  • 자료형/리터럴 관련: byte, short, int, long, float, double, char, boolean, void
  • 제어문/흐름 관련: if, else, switch, case, default, while, do, for, break, continue, return
  • 클래스/객체 관련: class, interface, enum, extends, implements, instanceof, new, super, this
  • 접근/제어: public, private, protected, static, final, abstract, synchronized, transient, volatile
  • 예외 처리: try, catch, finally, throw, throws
  • 기타: package, import, assert, goto, const
    goto/const는 실제로 사용되지는 않는다.
profile
선형의 비선형적 기록 🐜

0개의 댓글