출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]
ex) float + int = float + float

 

* byte + byte = int + int -> int

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
        /*
                                             우선순위
                                            
            boolean        1byte                낮음
                                          
           byte        1byte              
           short        2byte            
           int            4byte              
           long        8byte                   
                                           
           float        4byte               
            double        8byte                높음
            
            자료형의 변환 (auto, cast)
            
         */
        
        
        
        // 자동 자료형 변환 (auto)
        
        short sh = 12345;    // 2byte
        int i;                // 4byte
        
        i = sh; //자동형변환
        System.out.println("i = " + i);
        
        
        
        
        // 강제형 변환 (cast)
        
        i = 1234567891;
        
        // sh = i; 에러남!
        
        sh = (short) i; 
        System.out.println("sh = "+ sh);  
        // sh=723이 나온다.
        // 그러므로 형변환 할 때는 범위의 값을 잘 생각해서 변환해야한다.
        
        
        
        
        
        long l = 123456789L;
        float f = l;
        double d = l;
        
        System.out.println("f = " + f); // float라서 용량부족으로 여섯번째 숫자부터 에러남
        System.out.println("d = " + d); // 1.2345678E8
        
        //1.23456789E8
        //1.23456789 * 10^8
        
        double d1 = 1.234e2// 123.4 
        double d2 = 1.234e-2// 0.01234
        
        int n1, n2;
        n1=3;
        n2=2;
        
        double result = n1 / n2 ;  // 1.5가 나오지 않는다.
        double result2 = (double)n1/n2; 
        
        System.out.println(result); // 1.0 틀림
        System.out.println(result2); // 1.5 맞음
        
cs
 
 
 

+ Recent posts