JAVA 的基本数据类型

基本介绍

* 整数型
        * byte 占一个字节  -128到127
        * short 占两个字  -2^15~2^15-1
        * int 占四个字节 -2^31~2^31-1
        * long 占八个字节 -2^63~2^63-1

* 浮点型
        * float 占四个字节 -3.403E38~3.403E38  单精度
        * double 占八个字节-1.798E308~1.798E308 双精度

* 字符型
        * char 占两个字节 0~65535
* 布尔型
        * boolean   
        * boolean理论上是占八分之一个字节,因为一个开关就可以决定是true和false了,但是java中boolean类型没有明确指定他的大小    

 

混合运算时的类型转换

* 进行混合运算的时候,byte,short,char不会相互转换,都会自动类型提升为int类型,其他类型进行混合运算的是小的数据类型提升为大的

    * byte,short,char -- int -- long -- float -- double

class Demo3_DataTypeConversion {					//Conversion转换
	public static void main(String[] args) {
		//数据类型转换之隐式转换

		/*int x = 3;
		byte b = 4;

		x = x + b;

		System.out.println(x);*/

		//数据类型转换之强制转换
		/*int x = 3;
		byte b = 4;

		b = (byte)(x + b);
		System.out.println(b);*/

		//00000000 00000000 00000000 10000010			130的二进制
		//10000010										-126补码
		//00000001										-1求反码
		//10000001										-126反码
		//11111110										-126原码
		byte b = (byte)(126 + 4);
		System.out.println(b);
		//00000000 00000000 00000001 00101100			300的二进制
		//00101100
		byte b2 = (byte)300;
		System.out.println(b2);
	}
}
class Demo4_DataTypeConversion {
    public static void main(String[] args) {
        //System.out.println('a' + 1);            //98,因为有ASCII码表,a字符对应的是int类型的97
        //System.out.println((char)('a' + 1));

        System.out.println("hello"+'a'+1);        //任何数据类型用+与字符串相连接都会产生新的字符串
        System.out.println('a'+1+"hello");

        System.out.println(" 5 + 5 = " + (5 + 5));
    }
}
class Test1_DataTypeConversion {
    public static void main(String[] args) {
        //面试题:看下面的程序是否有问题,如果有问题,请指出并说明理由。
        byte b1 = 3;
        byte b2 = 4;
        //byte b3 = b1 + b2;
        /*
        从两方面
        1,byte与byte(或short,char)进行运算的时候会提升为int,两个int类型相加的结果也是int类型
        2,b1和b2是两个变量,变量存储的值是变化,在编译的时候无法判断里面具体的值,相加有可能会超出byte的取值范围
        */
        //System.out.println(b3);
        //byte b4 = 3 + 4;            //java编译器有常量优化机制
        byte b4 = 7;
        System.out.println(b4);
    }
}
class Demo5_Char {
    public static void main(String[] args) {
        char c = 'a';
        System.out.println(c);

        byte b = 100;
        char c1 = 97;        //0 - 65535
        System.out.println(c1);

        char c2 = 3;
        char c3 = 4;
        char c4 = 5;
        char c5 = 6;

        System.out.println(c2);
        System.out.println(c3);
        System.out.println(c4);
        System.out.println(c5);

        //char类型是否可以存储中文
        char c6 = '中';
        System.out.println(c6);
    }
}

 

float和long之间的对比  


class Test_DataTypeConversion {
    public static void main(String[] args) {
        float f = 12.3f;
        long x = 12345;

        f = x;                        //隐式转换
        System.out.println(f);

        x = (long)f;                    //强制转换
        System.out.println(x);

    }
}
 

* long: 8个字节

   

* float:4个字节

    * IEEE754

    * 4个字节是32个二进制位

    * 1位是符号位

    * 8位是指数位

    * -126到127

    * 23位是尾数位

    * 每个指数位减去127

    * A:它们底层的存储结构不同。

    * B:float表示的数据范围比long的范围要大

        * long:2^63-1

        * float:3.4*10^38 > 2*10^38 > 2*8^38 = 2*2^3^38 = 2*2^114 > 2^63-1

 

posted on 2017-05-24 21:51  平凡的小石头  阅读(148)  评论(0编辑  收藏  举报

导航