Java/Variable(변수)

자동형변환

Bohemian life 2012. 4. 6. 14:01
public class OperatorEx22 {
  public static void main(String[] args){
    int a = 10;
    float b = 10.0f;
    
    if(a ==b){//int -> float 자동형변환
            //10 -> 10.0f
           //10.0f == 10.0f
      System.out.println("10과 10.0f는 같다.");
      }
    char c = '0'; //아스키코드 48을 의미함
    int d = 0; 
    
    if(c != d){ //char -> int 자동형변환
          //'0' -> 48
          // 48 != 0
      System.out.println("'0'과 0은 같지 않다.");
    }
    
    char e = 'A'; //아스키코드 65
    int f = 65;
    if(e == f){ // char -> int
          // 'A' -> 65
          // 65  == 65
      System.out.println("'A'는 65와 같다.");
    }
    
    int num = 5;
    
    if( num > 0 && num < 9){
      //true        true  -> true
      System.out.println("5는 0보다 크고, 9보다는 작다.");
    }
  }
}