java 做题小细节

xiaoxiao2021-02-28  95

1:String 类型与基本数据类型用“+”连接: 部分代码:        System.out.println("hello"+3+4);//输出结果为:hello34        System.out.println(3+4+"hello");输出结果为:7hello        System.out.println('a'+3+"hello");//输出结果为:100hello        System.out.println("hello"+'a'+3); //输出结果为:helloa3 2:类型转换: short s = 5;    s = s -2;//编译不通过,int 转short 可能会有损失  ------------------------------------------------------------------         byte b = 3;        b = b + 4; //编译不通过,int 转byte 可能会有损失       b = (byte)(b+4);  --------------------------------------------------------------------         byte b = 5;        short s = 3;        short t = s + b; //编译不通过,int 转short 可能会有损失 -------------------------------------------------------------------- 3:double类型整数部分的0省略 char c = ‘a’;        int  i = 5;         double d = .314;//编译能通过         double result = c+i+d; ------------------------------------------------------------------------ 4:& 与&&    |与||          int x = 1;           int y=1;           if(x++==2 & ++y==2){ //非短路与,不管&前面真假,后面都要执行         x =7;           }           System.out.println("x="+x+",y="+y); //x=2 y=2 ------------------------------------------------------------------------------------         int x = 1,y = 1;          if(x++==2 && ++y==2){ //短路与,如果&&前面为真,则执行后面,否则,不执行后面       x =7;          }          System.out.println("x="+x+",y="+y); //x=2  y=1 --------------------------------------------------------------------------------------         int x = 1,y = 1;          if(x++==1 | ++y==1){ //非短路或,不管|前面真假,后面都要执行        x =7;          }          System.out.println("x="+x+",y="+y); //x=7 y=2 --------------------------------------------------------------------------------------         int x = 1,y = 1;          if(x++==1 || ++y==1){ //短路或,如果||前面为假,则执行后面,否则,不执行后面       x =7;          }          System.out.println("x="+x+",y="+y);//x=7 y=1 -------------------------------------------------------------------------------------
转载请注明原文地址: https://www.6miu.com/read-43243.html

最新回复(0)