java 做题小细节

来源:互联网 发布:淘宝低价风险交易 编辑:程序博客网 时间:2024/04/29 13:56

  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


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