본문 바로가기

Java/3. 데이터 타입(Data Type)

Java - 데이터 타입 종류

1. 정수타입(long, int, short, byte)

타입 숫자 범위 bit byte
long   -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807  64 8
int   -2,147,438,648 ~ 2,147,438,647 32 4
short   -32,768 ~ 32,767 16 2
byte   -128 ~ 127  8 1

 

public class Datatype {
	public static void main(String[] args) {
		// 정수(long, int, short, byte)
		long l = 64L; // -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 / 64bit / 8byte 
			      // long 타입에는 L을 붙혀준다
		int i = 32;   // -2,147,438,648 ~ 2,147,438,647 / 32bit / 4byte
		short s = 16; // -32,768 ~ 32,767 / 16bit / 2byte
		byte b = 8;   // -128 ~ 127 / 8bit / 1byte 
        }
}

2. 실수타입(float, double)

public class Datatype {
	public static void main(String[] args) {
		// 실수(double, float)
		double d = 1.1;
		float f = 10.11f; //float 타입에는 f를 붙혀준다
        }
}

3. 문자타입(char, String)

public class Datatype {
	public static void main(String[] args) {
		// 문자 1개(char)
		char c = 'A'; // 문자 1개 입력시 ''(작은따옴표) 사용
		System.out.println(c);
        
		// 문자열(String)
		String str1 = "Hello World!"; // 문자열 입력시 ""(큰따옴표) 사용
		System.out.println(str1);
        
		// 문자열 내에서 따옴표 사용시 \(역슬래시)+" 사용(이스케이프 문자)
		String str2 = "I said \"Apple\"";
		System.out.println(str2);
        }
}

출력결과

A
Hello World!
I said "Hello World!"


4. 논리타입(boolean)

public class Datatype {
	public static void main(String[] args) {
		// 논리타입(boolean)
		boolean tf = true; // true or false
        
		if(tf) { // if 조건문
    		System.out.println("참");
		} else {
    		System.out.println("거짓");
		}
}

출력결과

 

 

 

'Java > 3. 데이터 타입(Data Type)' 카테고리의 다른 글

Java - 데이터 타입 변환  (0) 2022.11.10