Java review(3) Java类型转换

  1. boolean 不能转换成其他类型,其他类型也不能转换成boolean
  2. 其它基本类型之间可以任意进行强制转换
@Test
    public void test1() throws Exception {
        // boolean 不能转换成其他类型,其他类型也不能转换成boolean
        boolean b = false;

        System.out.println("------------ char -----------");
        char c = 65;
        System.out.println(c);
        c = '1';
        System.out.println(c);
        // System.out.println((boolean) c);
        System.out.println((char)c);
        System.out.println((byte)c);
        System.out.println((short)c);
        System.out.println((int)c);
        System.out.println((long)c);
        System.out.println((float)c);
        System.out.println((double)c);

        // ------------ char -----------
        // A
        // 1
        // 1
        // 49
        // 49
        // 49
        // 49
        // 49.0
        // 49.0

        System.out.println("------------ short -----------");
        short s = 65;
        System.out.println(s);
        s = '1';
        System.out.println(s);
        // System.out.println((boolean) s);
        System.out.println((char)c);
        System.out.println((byte)s);
        System.out.println((short)s);
        System.out.println((int)s);
        System.out.println((long)s);
        System.out.println((float)s);
        System.out.println((double)s);

        // ------------ short -----------
        // 65
        // 49
        // 1
        // 49
        // 49
        // 49
        // 49
        // 49.0
        // 49.0

        System.out.println("------------ byte -----------");
        byte bte = 65;
        System.out.println(bte);
        bte = '1';
        System.out.println(bte);
        // System.out.println((boolean) bte);
        System.out.println((char)c);
        System.out.println((byte)bte);
        System.out.println((short)bte);
        System.out.println((int)bte);
        System.out.println((long)bte);
        System.out.println((float)bte);
        System.out.println((double)bte);

        // ------------ byte -----------
        // 65
        // 49
        // 1
        // 49
        // 49
        // 49
        // 49
        // 49.0
        // 49.0
		
		// ... 省略其他变量类型
    }
  1. 当你使用的转型常量大于该变量最大值时,则需要强制转型。下例中,char最大值是2 ^ 16 -1(无符号),当值大于65535时会要求显式进行转型。转型采用截取低位的方法。
    @Test
    public void test2() throws Exception {

        // char 最大值 65535
        System.out.println("char max: " + Math.round(Math.pow(2, 16) - 1));
        char a = (char) (65535 + 65);
        System.out.println(a);
        System.out.println((long) a);

        // char max: 65535
        // @
        // 64
    }
  1. 低精度向高精度转型无需强制转换,因为不会损失精度
  2. 高精度向低精度转型需强制转换,因为会损失精度
    Java和c语言不同,在不同系统上它的基础变量类型的位数也是一致的。这也正是它高度移植性的原因。
    在这里插入图片描述
    @Test
    public void test3() throws Exception {

        byte bte = (byte) 126;
        System.out.println(Byte.toString(bte));
        System.out.println(Integer.toBinaryString(bte));
        System.out.println(bte);

        // 向高精度转型无需强制转换
        double d = bte;

        System.out.println(d);

        d = 1234123412.123421341234;
        System.out.println(d);
        
        // 向低精度转型需强制转换
        bte = (byte) d;

        System.out.println(bte);

        // 126
        // 1111110
        // 126
        // 126.0
        // 1.2341234121234214E9
        // -108
    }
  1. 父类转子类会抛出ClassCastException,子类转父类则可以隐式转换,不会抛出异常
    class A {}
    class B extends A {}

    @Test
    public void test4() throws Exception {
        A a = new A();
        try {
            B b = (B) a;
        } catch (ClassCastException e) {
            System.out.println(e.getMessage());
        }

        B b = new B();
        A aa = b;
        
        // cn.hengyumo.review.r1.Test1$A cannot be cast to cn.hengyumo.review.r1.Test1$B
    }
  1. 实现同一个接口的父子类同样无法直接转型
    interface I {}
    class D implements I {}
    class E extends D implements I {}
    @Test
    public void test5() throws Exception {
        D d = new D();

        try {
            E e = (E) d;
        } catch (ClassCastException e) {
            System.out.println(e.getMessage());
        }

        I i = d;

        try {
            E e = (E) i;
        } catch (ClassCastException e) {
            System.out.println(e.getMessage());
        }
        
        // cn.hengyumo.review.r1.Test1$D cannot be cast to cn.hengyumo.review.r1.Test1$E
        // cn.hengyumo.review.r1.Test1$D cannot be cast to cn.hengyumo.review.r1.Test1$E
    }
  1. 包装类之间可以任意转换,但是基础类型转高精度类型的包装类时无法再进行隐式转换
	@Test
    public void test6() throws Exception {
        int a = 2;

        Integer i = a;
        int aa = i;

        // cannot cast 'int' to 'java.lang.Long'
        // Long l = (Long) a;
        Long l = (long) a;
        long ll = l;

        // cannot cast 'int' to 'java.lang.Short'
        // Short s = (Short) a;
        Short s = (short) a;
        short ss = s;

        // Long lll = s;
        // cannot cast 'java.lang.Short' to 'java.lang.Long'
        // Long lll = (Long) s;
    }
  1. 包装类为null时转换基本类型会抛出空指针异常
    @Test
    public void test7() throws Exception {
        Boolean b = null;

        try {
            boolean bb = b;
        } catch (NullPointerException e) {
            System.out.println(e.getMessage());
        }

        Integer i = null;

        try {
            long l = i;
        } catch (NullPointerException e) {
            System.out.println(e.getMessage());
        }
        
        // null
        // null
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值