728x90
데이터 타입이 존재하는 이유는 코드를 작성하면 메모리에 저장된다. 하지만 메모리는 유한하기 때문에 코드를 쓸 때마다 메모리가 확보된다. 메모리는 한정적이기 때문에 최적화시켜야한다. (자바에는 크게 신경 쓸 필요는 없으나 C언어를 할 때는 메모리 신경을 써야한다.)
데이터 타입이 뭐냐에 따라서 처리할 수 있는 값의 범위가 달라진다
컴퓨터는 1과 0 이진수만 인식한다.
Byte : 1Byte = > 8bit => 2의 8제곱
256 -> -128 ~ 127
class Main{
public static void main(String[] args) {
// 데이터 타입 : 숫자형, 문장형
// 숫자형 : 정수형, 실수형
System.out.println("== 정수형 데이터 타입 ==");
// byte, short, int, long
byte by = 127;
// byte aa = 256; 저장 불가
// byte bb = -128; 값의 범위 : -128 ~ 127
System.out.println("by : " + by);
System.out.println("byte : " + Byte.BYTES); // 1바이트
short s = 32767;
System.out.println("s : " + s); // 값의 : -32768 ~ 32767
System.out.println("short : " + Short.BYTES); // 2바이트
int a = 214748364;
System.out.println("a : " + a); // 값의 : -214783648 ~ 2147483647
System.out.println("int : " + Integer.BYTES); // 4바이트 -> 32비트
long l = 9223372036854L; // 값의 범위가 큼 뒤에 L을 붙여줘야한다.
System.out.println("l : " + l);
System.out.println("long : " + Long.BYTES); // 8바이트 -> 64비트
System.out.println("== 실수형 데이터 타입 ==");
//float, double
float f =3.141592653589793f;
System.out.println("f : " + f);
System.out.println("float : " + Float.BYTES); // 4바이트 -> 32비트
double d =3.141592653589793;
System.out.println("d : " + d);
System.out.println("double : " + Double.BYTES); // 8바이트
System.out.println("== 논리형 데이터 타입 ==");
// boolean
boolean bool1 = true;
boolean bool2 = false;
System.out.println("bool1 : " + bool1);
System.out.println("bool2 : " + bool2);
System.out.println(" == 문자형 데이터 타입 ==");
// char
char c = 'H';
System.out.println("c : " + c);
System.out.println("char : " + Character.BYTES);
System.out.println("== 문장형 데이터 타입 ==");
String str = "안녕하세요.";
System.out.println("str : " + str);
}
}
728x90
'자바(Java)' 카테고리의 다른 글
자바(Java)(4) - 반복문 while (0) | 2023.09.17 |
---|---|
자바(Java)(3) - 연산자, if, else if, else 문 (1) | 2023.09.17 |
자바(Java)(1) - 변수 (0) | 2023.09.16 |