![[Java] Primitive type (기본형 타입)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fdv1jzN%2FbtsoUMp1ijJ%2FQK8Of97dQgXdg7LlsxeP2K%2Fimg.png)

Primitive type
- 총 8가지의 기본형 타입을 자바에서 제공함
- 기본값을 제공하기 때문에 Null이 존재하지 않음. 만약 기본형 타입에 Null을 넣고 싶은 경우 Wrapper class를 활용해야함
- 실제 값을 저장하는데, 이때 Stack(스택) 메모리에 저장됨
타입 | 할당 메모리 크기 | 데이터 표현 범위 | |
논리형 | boolean | 1byte | false, true |
정수형 | byte | 8bit (1byte) | -128 ~ 127 |
short | 16bit (2byte) | -32,768 ~ 32,767 | |
int | 32bit (4byte) | -2,147,483,648 ~ 2,147,483,647 | |
long | 64bit (8byte) | -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 | |
실수형 | float | 32bit (4byte) | (3.4 X 10-38) ~ (3.4 X 1038) 의 근사값 |
double | 64bit (8byte) | (1.7 X 10-308) ~ (1.7 X 10308) | |
문자형 | char | 2byte | 0 ~ 65,535 ('\u0000' ~ '\uffff') |
오버 플로우(overflow)
해당 데이터 타입이 표현할 수 있는 최대 범위보다 큰 수를 저장할 때 발생하는 현상
언더 플로우(underflow)
해답 데이터 타입이 표현할 수 있는 최소 범위보다 작은 수를 저장할 때 발생하는 현상
Default Value (기본값)
DataType | Default value |
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | '\u0000' |
boolean | false |
String의 경우 primitive type 처럼 사용가능하지만 primitive type은 아님 (default : null)
Literals (리터럴)
Primitive types are special data types built into the language; they are not objects created from a class. A
literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation.
boolean result = true;
char capitalC = 'C';
byte b = 100;
short s = 10000;
int i = 100000;
직접 표현(고정)한 값을 그자체 뜻함 (정수/실수/문자 리터럴 등등)
Character and String Literals
- 컴퓨터의 경우 2진수(0, 1) 밖에 인식하지 못하므로 문자 또한 숫자로 표현해야 인식 가능
- 자바에서는 유니코드(unicode, 2byte = 2^16 = 65,536개)을 사용하여 문자를 표현
Always use 'single quotes' for char literals and "double quotes" for String literals
char 의 경우 single quotes(''), String 은 double quotes("")로 구분 (처음 알게 됨 :0)
자바에서 지원하는 특수문자 리터럴(speical escape sequence)
종류 | 의미 | 종류 | 의미 |
'\b' | backspace | '\r' | carriage return, CR |
'\t' | tab | '\"' | double quote, 이중 인용부호 |
'\n' | Line Feed, LF | '\'' | single quote, 단일 인용부호 |
'\f' | 폼피드(form feed) | '\\' | backslash |
참고
https://jw910911.tistory.com/90
개발 용어 : 캐리지 리턴(CR), 라인 피드 (LF) 알아보기
캐리지 리턴(CR)과 라인 피드(LF)란? 프로그래밍을 하다보면 \n으로 되어있는 문자를 만나게 됩니다. 보통 줄내림이라는 뜻인데 윈도우에서 이것저것 하다보면 \n이 아닌 \r\n도 만날 때가 있습니다
jw910911.tistory.com
Window는 CRLF로 개행처리하는데, Unix 같은 경우 LF 만으로 개행처리 되는거였구나..
Git Clone 받을 때 개행문자 이슈 원인을 이제야 이해 :0
참고
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
'공부 > Java' 카테고리의 다른 글
[Java] Enum values 배열을 리스트 변환 (Baeldung, Enum values to List) (0) | 2023.07.21 |
---|---|
[Java] Generics (제네릭) - 공변/무공변/반공변, PECS (0) | 2023.07.21 |
[Java] Annotation - 어노테이션 (자바의 정석) (0) | 2023.07.19 |
[Java]Comparable 과 Comparator 인터페이스 (0) | 2021.09.30 |
[Java] Collection Framework (콜렉션 프레임워크) (0) | 2021.09.23 |

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!