荣雪---Jusenr

QQ:917641472

数据类型转换(面试题)

 

1
/* 2 面试题: 3 4 byte b1=3,b2=4,b; 5 b=b1+b2; 6 b=3+4; 7 那句是编译失败 的呢?为什莫尼? 8 9 遇到这样的题目主要看等号右边的表达式的形式,变量相加还是常量相加 10 11 */ 12 class VariableTest 13 { 14 public static void main(String[] args) 15 { 16 byte b1=3,b2=4,b,bb; 17 //b=b1+b2;错误 错误,可能损失精度 18 bb=(byte)(b1+b2); 19 /* 20 以上代码的步骤分析: 21 byte 有三个变量:b1 b2 b 22 等号是把b1+b2做加法操作(类型 比int类型小,变量在做加法的时候要找到最大数值类型,没有long、没有float、没有double)int类型 23 把操作后的结果int 类型给了(byte)类型,强制类型转换 24 25 26 */ 27 b=3+4;//正确 b=byte 7 28 /* 29 以上代码的步骤分析: 30 3是一个人常量,4是一个常量。常量加常量结果还是常量 31 注意: 32 在编译时就能决定程序是否错误。会把常量值判断在不在当前数据类型的范围内,会把当前的常量值看成我所属的类型 33 */ 34 System.out.println(bb); 35 System.out.println(b); 36 37 } 38 }


----------------------------------------------------------------------------------------------------

 1 /*
 2     掌握里面隐含的知识,面试考的较多
 3 
 4     System.out.println('a');
 5     System.out.println('a'+1);
 6     System.out.println("hello"+'a'+1);
 7     System.out.println('a'+1+"hello");
 8     System.out.println("5+5"+5+5);
 9     System.out.println(5+5+"=5+5");
10     
11     byte、short-->int
12     char
13 
14     注意:遇到字符串和任何类型做加法计算,结果一定变成字符串类型(做拼接)
15 
16 */
17 class  VariableDemo6
18 {
19     public static void main(String[] args) 
20     {
21         System.out.println('a');//看到的和得到的相同
22         System.out.println('a'+1);//隐藏了数据类型的自动提升转换  98
23         /*
24             我们输出内容的顺序和中国人阅读的顺序相同
25             从左向右进行输出将会依次按照以下步骤进行:
26                 1、"hello"+'a'="helloa"
27                 2、"helloa"+1="helloa1"
28         */
29         System.out.println("hello"+'a'+1);
30         //98hello
31         System.out.println('a'+1+"hello");
32         //5+5=55
33         System.out.println("5+5="+5+5);
34         //10=5+5
35         System.out.println(5+5+"=5+5");
36     }
37 }
----------------------------------------------------------------------------------------------------

 1 /*
 2     面试题
 3     short s=1,s=s+1;short s=1,s+=1;
 4     上面两个代码有没有问题,如果有,那里有问题
 5 
 6     byte-->        short-->    int-->    long-->    float-->    double
 7     最小        较小        标准值    较大    更大        最大
 8 
 9     扩展赋值运算符会帮你做强制转换
10 */
11 class  OperateText2
12 {
13     public static void main(String[] args) 
14     {
15         short s=1;
16         //s=s+1;//提升s为int 类型,等号右边int 是4字节(损失精度的问题)
17 
18         s+=1;//s=(short)(s+1);
19         System.out.println("s="+s);
20     }
21 }

----------------------------------------------------------------------------------------------------

 1 import java.util.Scanner;
 2 /*
 3         面试题版本1:
 4         int x = 2;
 5         int y = 3;
 6         switch(x){//x=2
 7             default:
 8                 y++;
 9                 break;
10             case 3:
11                 y++;
12             case 4:
13                 y++;
14         }
15         System.out.println("y="+y);
16 
17 
18 
19         面试题版本2:
20         int x = 2;
21         int y = 3;
22         switch(x){//x=2
23             default:
24                 y++;
25             case 3:
26                 y++;
27             case 4:
28                 y++;
29         }
30         System.out.println("y="+y);
31 */
32 class  SwitchTest
33 {
34     public static void main(String[] args) 
35     {
36         Scanner jianpan = new Scanner(System.in);
37 
38         //人机交互(软件界面)
39         System.out.println("请选择你的女神:");
40         System.out.println("A: 范冰冰");
41         System.out.println("B: 刘诗诗");
42         System.out.println("C: 苍老师");
43         System.out.println("D: 柳岩");
44         //char ch = jianpan.nextChar();//错误,找不到符号
45         int num = jianpan.nextInt();
46         char ch = (char)num;
47 
48         
49         //软件业务逻辑
50         switch(ch){//case是并列关系,先后顺序可以变化
51             case 'A':
52                 System.out.println("你选择的是:范冰冰");
53                 break;
54             case 'B':
55                 System.out.println("你选择的是:刘诗诗");
56                 break;
57             case 'C':
58                 System.out.println("你选择的是:苍老师");
59             break;
60             case 'D':
61                 System.out.println("你选择的是:柳岩");
62             break;
63 
64         }
65     }
66 }

 

 

posted on 2016-01-25 01:32  Jusenr/荣雪-rongsnow  阅读(737)  评论(0编辑  收藏  举报

导航