java数据类型转换那点事

public class kkk {

    
/**
 * 先看看eclipse对于数值型转换会有哪些报错,但是有一点必须明确,eclipse不报错的,不一定就是说这种思维逻辑是对的    

 * 可以直接将代码复制过去在编译器里显示的明明白白

 * 红色代码表示编译会报错
 */
    public static void main(String[] args) {
//         byte 2个
         byte b1=(short)1;                //不准确的书写方式,如果超出byte范围就会报错
         byte b2=(int)3;                //不准确的书写方式,如果超出byte范围就会报错
         byte b3=(long)3;
         byte b4=(float)3;
         byte b5=(double)3;
//         short 2个
         short b6=(byte)300;
         short b7=(int)3;                //不准确的书写方式,如果超出byte范围就会报错
         short b8=(long)3;
         short b9=(float)3;
         short b10=(double)3;
//         int 2个
         int b11=(short)1;
         int b12=(byte)3;
         int b13=(long)3;
         int b14=(float)3;
         int b15=(double)3;
//         long 3个
         long b16=(byte)3;
         long b17=(int)3;
         long b18=(short)3;
         long b19=(float)3;
         long b20=(double)3;
//         float 4个
         float b21=(short)1;
         float b22=(byte)3;
         float b23=(long)3;
         float b24=(int)3;
         float b25=(double)3;
//         double 5个
         double b26=(byte)3;
         double b27=(int)3;
         double b28=(short)3;
         double b29=(float)3;
         double b30=(long)3;
//                  取值范围从小到大<精度由低到高>:byte->short->int->long->float->double
//                  赋值时,始终遵循大范围可以传递给小范围<高精度可以传递给低精度>,
//                  倘若大范围的将一个在小范围之内的数传递小范围的,直接赋值的方式不会出错,但是传递赋值就会出错
//                  例如byte b2=(int)3;不会报错,但是这种思维逻辑是错的;验证:int a=3;byte b2=a;此时出错
         
         
//-------在进行数字运算时,运算完后会取精度最高的作为结果的数据类型,但是int以下的都会转换为int类型---------------
         byte c1=1;
         byte c2=c1+1;            //byte+1之后的数据类型为int  
         
         short s=1;
         byte cc=c1+s;            //byte+short之后的数据类型为int
         
         float s1=1;
         byte cc1=c1+s1;        //byte+float之后的数据类型为float
         
         short c3=1;
         short c4=c3+1;            //short+1之后的数据类型为int
         
         float c5=1;
         float c6=c5+1;            //float+1之后的数据类型为float
         
         byte a1=1;                    //a1为byte型
         byte a2=2;                    //a2为byte型
         byte a3=a1+a2;                //错误提示为不可以将int型赋值给byte
         
         byte a21=(short)1;                //a21为byte型
         byte a22=(short)2;                //a22为byte型
         byte a23=a1+a2;                //错误提示为不可以将int型赋值给byte
         
         byte a11=1;                    //a11为byte型
         byte a12=2;                    //a12为byte型
         short a13=a11+a12;                //错误提示为不可以将int型赋值给short
         
         double a4=(byte)3;
         double a5=(short)3;
         double a6=a4+a5;
         int    a7=a4+a5;                //错误提示为不可以将double型赋值给int
         
//-------<a+=1;与a=a+1;的数据类型转换方式是不一样的---------->
         float a31=3;
         float a32=3;
         a31+=1;            //a31+=1;等价于a31=(float)(a31+1)
         a32=a32+1;            //而a32+1会自动转换为float型
         
         byte a41=3;
         byte a42=3;
         a41+=1;            //a41+=1;等价于a41=(byte)(a41+1)
         a42=a42+1;            //而a42+1会自动转换为int型
         
         byte a51=3;
         byte a52=3;
         a51+=1;            
         a52=a52%1;            //同样的,对于+-*/%同样遵循以上数据类型转换规则
        

//----------数字和字符串之间转换------------------
         
            //1.字符串转换为数字
            String str="123";
            String str2="123.11";
            int w3;
            float e3;
            w3=Integer.parseInt(str);
            e3=Float.parseFloat(str2);
            System.out.println("字符串转换为int类型 的w3="+w3);
            System.out.println("字符串转换为float类型 的e3="+e3);
            
            //2.数字转换为字符串
            int w4=100;
            float e4=123.11f;
            String str3,str4;
            str3=String.valueOf(w4);
            str4=String.valueOf(e4);
            System.out.println("int类型转换为字符串 的str3="+str3);
            System.out.println("float类型转换为字符串 的str4="+str4);


    }

}

//以上内容仅供个人参考学习,如有错误请联系本人。thanks!

posted @ 2017-06-28 08:46  大向无形  阅读(151)  评论(0编辑  收藏  举报