day06 数据类型扩展及面试题讲解

数据类型扩展及面试题讲解

public class dome03 {
    public static void main(String[] args) {
        //整数拓展      进制  二进制0b 十进制 八进制0 十六进制0x

        int i = 10;
        int i2 = 010;//八进制
        int i3 = 0x10;//十六进制0x    0~9   A~F 16
        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println("================================================");
        //=========================================================
        //浮点数拓展?    银行业务怎么表示? 钱
        //bigdecimal    数学工具类
        //=========================================================
        //float 有限  离散  舍入误差    大约  接近但不等于
        //double
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较

        float f = 0.1f;//0.1
        double d = 1.0/10; //0.1
        System.out.println(f==d);//返回false

        float d1 = 23123232131312312312f;
        float d2 = d1  + 1;
        System.out.println(d1);
        System.out.println(d2);
        System.out.println(d1==d2);

        //=========================================================
        //字符串拓展?
        //=========================================================
        System.out.println("=======================================================");
        char c1 = 'A';
        char c2 = '中';

        System.out.println(c1);

        System.out.println((int)c1);//强制转换

        System.out.println(c2);

        System.out.println((int)c2);//强制转换

        //所有的字符本质还是数字
        //编码    unicode 表:(97 = a   65 = A) 2字节    0 - 65536   Excel   2 16 = 65536
        //  U0000   UFFFF

        char c3 = '\u0061';

        System.out.println(c3);//a

        //转义字符
        //  \t  制表符
        //  \n  换行
        //  \a  响铃
        //  \b  退格
        //  \f  换页
        //  \r  回车
        //  \v  垂直制表
        

        System.out.println("Hello\nWorld");

        //骚操作
        System.out.println("=======================================================");
        String sa = new String("hello world");
        String sb = new String("hello world");
        System.out.println(sa==sb);

        String sc = "hello world";
        String sd = "hello world";
        System.out.println(sc==sd);
        //对象    从内存分析

        //布尔值拓展
        boolean flag = true;
        if (flag==true){}   //新手
        if (flag){} //老手
        //less is more! 代码要精简易读

    }
}

类型转换

  • 由于Java是强类型语言,所以要进行有些运算的时候的,需要用到类型转换
    • 低-----------------------------------------------> 高
    • byte,short,char-> int-> long-> float-> double
  • 运算中,不同类型的数据先转化为同一类型,然后进行运算。
  • 强制类型转换
  • 自动类型转换
public class demo04 {
    public static void main(String[] args) {
        int i = 128;
        byte b = (byte) i;//内存溢出

        //强制转换  (类型)变量名 高--低
        //自动转换  低--高
        double c = i;
        //boolean d = i;

        System.out.println(i);
        System.out.println(b);
        System.out.println(c);

        /*
        注意点:
        1.不能对布尔值进行转换
        2.不能把对象类型转换为不想干的类型
        3.在把高容量转换到低容量的时候,强制转换
        4.转换的时候可能存在内存溢出,或者精度问题!
         */

        System.out.println("==========================================================");
        System.out.println((int) 23.7);
        System.out.println((int)-45.89f);

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

    }
}

public class demo05 {
    public static void main(String[] args) {
        //操作比较大的数的时候,注意溢出问题
        //JDK7新特性,数字之间可以用下划线分割
        int money = 10_0000_0000;
        System.out.println(money);
        int year = 20;
        int total = money*year;//-1474836480,计算时候溢出了
        int total2 = money*year;//默认是int,转换之前已经存在问题了
        System.out.println(total);
        System.out.println(total2);

        long total3 = money*((long)year);
        System.out.println(total3);
        
        //  l   L
    }
}

posted @ 2021-02-28 22:56  圈圈子  阅读(53)  评论(0)    收藏  举报