본문 바로가기
JAVA

2일차// 자료형 변환

by aesup 2021. 1. 12.
728x90
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
public class MainClass {
   public static void main(String[] args) {
    /*
     (자료형 변환)    byte    
     boolean       1
                           우선순위
     byte          1         낮음
     short         2
     int           4
     long          8
     
     float         4
    double        8         높음 
     
    
     
     */
        //자동변환
       short sh = 12345;  //sh 를 num으로 들이부은다
       int num = sh ;
       System.out.println(num);
       
       num = 23456;
       sh = (short)num;  //강제형 변환
       
       //자동변환(우선순위가 높기때문에 자동으로 변환)
       
       //강제형 변환이 필요할때
       
       float f1;
       f1 = (3 / 2);
       System.out.println(f1); //1.0이나온다 이렇게 하면!!
       
       
       float f2;
       f2 = (float)3 / 2;
       System.out.println(f2);  // 1.5 제대로 출력
}
}
 
cs

" target="_blank" rel="noopener" data-mce-href="http://

 

">http://

 

728x90