HYOJIN LIM
by HYOJIN LIM
~1 min read

Categories

CH02. 변수와 타입

  1. 변수

    값을 저장할 수 있는 메모리 공간

  2. 기본형 변수(Primitive)

    메모리 정수형 실수형 논리형
    1byte byte   boolean
    2byte short, char    
    4byte int float (리터럴문자 F / f)  
    8byte long (리터럴문자 L / l) double  
    • char: 양수의 값만 갖는다 (유니코드 사용)

    • 정수형의 기본 타입은 int이다.

    • 실수형의 기본타입은 double이다.

  3. 변수의 타입 변환

  • 자동 타입 변환(묵시적 타입 변환)

    작은 크기를 가지는 타입이 큰 크기를 가지는 타입에 저장될 때

  • 강제 타입 변환(명시적 타입 변환)

    큰 데이터 타입을 작은 데이터 타입으로 쪼개어서 저장하는 것

    public class valueTest {
      
    	public static void main(String[] args) {
    		byte n1 = 10;
    		byte n2 = 20;
      		
    //		byte n3 = n1 + n2;  : 정수 연산일 경우, int 타입을 기본으로 하기 때문에 byte로 형변환 해야 한다
      		
    		byte n3 = (byte)(n1 + n2);
      		
    		byte byteValue = 10;
    		char charValue = 'A';
      		
    //		short shortValue = charValue; : char형은 양수의 값을 가지므로 short로 형변환 해야 한다
      		
    		short shortValue = (short) charValue;
      		
    //		char emptyChar = ''; : char 형은 빈 문자열로 선언할 수 없다
      		
    	}
      
    }