类型转换拓展

package com.company;

public class Demo5 {
    public static void main(String[] args) {
        System.out.println("类型转换===========================");
        System.out.println("byte 1字节=>short 2,char 2=》int 4==>long 8==>float 4==>double 8=================小数高于整数的");
        int i=128;
        byte b=(byte)i;//内存溢出
        double b1=i;
        //Byte右键看源码  byte -128~127
        System.out.println(i);
        System.out.println(b);
        System.out.println(b1);
        System.out.println("================注意");
        /*
        * 注意点:
        * 1.不能对boolean转换
        * 2.不能把对象类型转换为不相干的类型
        * 3.大变小,强制转换
        * 4.转换的时候可能存在内存溢出,或者精度问题!
        * */
        int i1=128;
        //boolean b111=i; Error:(20, 22) java: 不兼容的类型: int无法转换为boolean
        System.out.println("===========(int)=============");
        System.out.println((int)23.7);
        System.out.println((int)-45.89f);

System.out.println("char=================");
char c='a';
int d=c+1;
System.out.println(d);
System.out.println((char)d);//int转char
        

    }
}

类型转换===========================
byte 1字节=>short 2,char 2=》int 4==>long 8==>float 4==>double 8=================小数高于整数的
128
-128
128.0
================注意
===========(int)=============
23
-45

char=================
98
b

加qq群422464063学习

猜你喜欢

转载自blog.csdn.net/wanggang182007/article/details/121003607