인프런 스터디/예제로 공부하는 Java 100 문제풀이 Part.1

자료형, 연산자 - 14: 기본형, 타입, 초기화 (1)

anthurium 2021. 7. 13. 21:18

기본형 타입의 값을 초기화하는 방법

 

기본형 타입의 값을 초기화한 아래의 코드에서 틀린 곳을 찾아 수정

 

public class Java100_variable_DataType2{
	public static void main(String[] args){
    
        // [1]: 기본형 타입 --> 8개 --> 변수 선언과 동시에 값을 입력
		byte b = 100;
		short s = 30000;
		int i = 2100000000;
        long l = 7000000000;
        float f = 9.8;
        double d = 3.14;
        char c = 'A';
        boolean bl = false;
        
        // [2]: 출력
        System.out.println(b);
        System.out.println(s);
        System.out.println(i);
        System.out.println(l);
        System.out.println(f);
        System.out.println(d);
        System.out.println(c);
        System.out.println(bl);
        
	}
}

오류발생:

long l = 7000000000;     -->    long l = 7000000000L;  
float f = 9.8;                 -->    float f = 9.8F;

 

long을 의미하는 접미사 l 혹은 L을 붙여줘야한다.

float를 의미하는 접미사 f 혹은 F를 붙여줘야한다.

보통 l은 1과 헷갈릴 수 있기 때문에 대문자를 선호한다. 

 

 

 

public class Java100_variable_DataType2{
	public static void main(String[] args){
    
        // [1]: 기본형 타입 --> 8개 --> 변수 선언과 동시에 값을 입력
		byte b = 100;
		short s = 30000;
		int i = 2100000000;
        long l = 7000000000L;
        float f = 9.8F;
        double d = 3.14;
        char c = 'A';
        boolean bl = false;
        
        // [2]: 출력
        System.out.println(b);
        System.out.println(s);
        System.out.println(i);
        System.out.println(l);
        System.out.println(f);
        System.out.println(d);
        System.out.println(c);
        System.out.println(bl);
        
	}
}​

출력결과:

100
30000
2100000000
7000000000
9.8
3.14
A
false

 

 

범위를 벗어나는 값을 입력하는 경우

public class Java100_variable_DataType2{
	public static void main(String[] args){
    
        // [1]: 기본형 타입 --> 8개 --> 변수 선언과 동시에 값을 입력
		byte b = 127;
		short s = 30000;
		int i = 2100000000;
        long l = 7000000000L;
        float f = 9.8F;
        double d = 3.14;
        char c = 'A';
        boolean bl = false;
        
        // [2]: 출력
        System.out.println(b+"byte 최대값 -->"+Byte.MAX_VALUE);
        System.out.println(s);
        System.out.println(i);
        System.out.println(l);
        System.out.println(f);
        System.out.println(d);
        System.out.println(c);
        System.out.println(bl);
        
	}
}

출력결과:

127 byte 최대값 --> 127
30000
2100000000
7000000000
9.8
3.14
A
false

 

public class Java100_variable_DataType2{
	public static void main(String[] args){
    
        // [1]: 기본형 타입 --> 8개 --> 변수 선언과 동시에 값을 입력
		byte b = 127;       // 128 오류
		short s = 32767;    // 32768 오류
		int i = 2147483647; // 2147483648 오류
        long l = 7000000000L;
        float f = 9.8F;
        double d = 3.14;
        char c = 'A';
        boolean bl = false; // 0,1도 안되며 대문자 True, False도 안된다.
        
        // [2]: 출력
        System.out.println(b+"byte 최대값 -->"+Byte.MAX_VALUE);
        System.out.println(s+"short 최대값 -->"+Short.MAX_VALUE);
        System.out.println(i+"int 최대값 -->"+Integer.MAX_VALUE);
        System.out.println(l);
        System.out.println(f);
        System.out.println(d);
        System.out.println(c);
        System.out.println(bl);
        
	}
}

출력결과:

127 byte 최대값 --> 127
32767 short 최대값 --> 32767
2147483647 int 최대값 --> 2147483647
7000000000
9.8
3.14
A
false