Java 기본 데이터 타입 탐구

JavaBeginner
지금 연습하기

소개

이 랩에서는 Java 의 여덟 가지 기본 데이터 타입에 대해 배우게 됩니다. 각 타입의 크기, 범위, 그리고 기본값에 대해 알아볼 것입니다. 또한 오토박싱 (autoboxing) 에 대해서도 배우게 됩니다.

새 Java 파일 생성

다음 명령을 사용하여 새로운 Java 파일을 생성하고 이름을 PrimitivesLab.java로 지정합니다.

touch PrimitivesLab.java

int 및 byte 데이터 타입 선언

i라는 이름의 int 데이터 타입과 b라는 이름의 byte 데이터 타입을 선언합니다. 각각 1510 값을 할당합니다.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
    }
}

short 및 long 데이터 타입 선언

s라는 이름의 short 데이터 타입을 선언하고 값 1000을 할당합니다. l이라는 이름의 long 데이터 타입을 선언하고 값 9999999L을 할당합니다.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
        System.out.println("Value of s: " + s);
        System.out.println("Value of l: " + l);
    }
}

float 및 double 데이터 타입 선언

f라는 이름의 float 데이터 타입을 선언하고 값 3.14f를 할당합니다. d라는 이름의 double 데이터 타입을 선언하고 값 2.71828을 할당합니다.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
        System.out.println("Value of s: " + s);
        System.out.println("Value of l: " + l);
        System.out.println("Value of f: " + f);
        System.out.println("Value of d: " + d);
    }
}

boolean 및 char 데이터 타입 선언

bool이라는 이름의 boolean 데이터 타입을 선언하고 값 true를 할당합니다. c라는 이름의 char 데이터 타입을 선언하고 값 'A'를 할당합니다.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        boolean bool = true;
        char c = 'A';
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
        System.out.println("Value of s: " + s);
        System.out.println("Value of l: " + l);
        System.out.println("Value of f: " + f);
        System.out.println("Value of d: " + d);
        System.out.println("Value of bool: " + bool);
        System.out.println("Value of c: " + c);
    }
}

오토박싱 (Autoboxing) 시연

x라는 이름의 Integer 데이터 타입을 선언하고 값 25를 할당하여 오토박싱을 시연합니다.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        boolean bool = true;
        char c = 'A';
        Integer x = 25;
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
        System.out.println("Value of s: " + s);
        System.out.println("Value of l: " + l);
        System.out.println("Value of f: " + f);
        System.out.println("Value of d: " + d);
        System.out.println("Value of bool: " + bool);
        System.out.println("Value of c: " + c);
        System.out.println("Value of x: " + x);
    }
}

정수 언더플로우 (Integer Underflow) 시연

n이라는 이름의 정수 데이터 타입을 선언하고 값 2147483647을 할당합니다. n1만큼 증가시키고 출력을 확인합니다.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        boolean bool = true;
        char c = 'A';
        Integer x = 25;
        int n = 2147483647;
        n = n + 1;
        System.out.println("Value is: " + n);
    }
}

정수 오버플로우 (Integer Overflow) 시연

m이라는 이름의 정수 데이터 타입을 선언하고 값 -2147483648을 할당합니다. m1만큼 감소시키고 출력을 확인합니다.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        boolean bool = true;
        char c = 'A';
        Integer x = 25;
        int n = 2147483647;
        n = n + 1;
        System.out.println("Value is: " + n);
        int m = -2147483648;
        m = m - 1;
        System.out.println("Value is: " + m);
    }
}

프로그램 컴파일 및 실행

터미널에서 다음 명령을 사용하여 프로그램을 컴파일합니다.

javac PrimitivesLab.java

다음 명령을 사용하여 프로그램을 실행합니다.

java PrimitivesLab

요약

이 랩에서는 Java 의 8 가지 기본 데이터 타입 (primitive data types) 에 대해 배웠습니다. Java 프로그램을 생성하고 각 기본 데이터 타입의 변수를 선언했습니다. 오토박싱 (autoboxing) 과 Java 가 기본 데이터 타입을 해당 래퍼 클래스 (wrapper class) 로 자동 변환하는 방법에 대해 배웠습니다. 또한 정수 데이터 타입의 오버플로우 (overflow) 및 언더플로우 (underflow) 에 대해서도 배웠습니다. 마지막으로, 출력을 확인하기 위해 프로그램을 컴파일하고 실행했습니다. 이러한 데이터 타입을 프로그래밍에서 사용하여 데이터를 효율적으로 저장하고 조작할 수 있습니다.