首页 > 技术文章 > java基础:数据类型拓展

zhsheng 2021-03-04 10:11 原文

public static void main(String[] args) {
        //单行注释
        //输出hello,world!
        //System.out.println("hello,world!");

        //整数拓展
        int i=10;//十进制
        int i1=010;//8进制0
        int i2=0x10;//16进制0x
        System.out.println(i);
        System.out.println(i1);
        System.out.println(i2);

        System.out.println("=========================");

        //浮点数拓展
        //银行业务怎么表示?钱 float?double
        //BigDecimal数学工具类
        float f=0.1f;
        double d=1.0/10;
        System.out.println(f==d);//输出了false,为什么?
        System.out.println(f);
        System.out.println(d);

        float f1=23233232323232323232f;
        float f2=f1+1;
        System.out.println(f1==f2);//输出了true,为什么?
        /*
        原因:
        float:有限 离散 含有误差 大约 接近但不等于
        最好完全避免使用浮点数进行比较
        最好完全避免使用浮点数进行比较
        最好完全避免使用浮点数进行比较
         */

        System.out.println("=========================");

        //字符拓展
        char a='a';
        char b='张';
        System.out.println(a);
        System.out.println((int)a);//强制类型转换
        System.out.println(b);
        System.out.println((int)b);//强制类型转换,所有字符本质上还是数字
        /*
        编码 Unicode u0000-uffff
        ASCII表(97=a;65=A)
        char占2字节 可表示0-63336
         */
        char c='\u0061';// '\'表示转义
        System.out.println(c);
        /*
        转义字符
        \t \n 等
         */

        System.out.println("=========================");

        String str=new String("hello,world");
        String str1=new String("hello,world");
        System.out.println(str==str1);//false
        String str2="zdz";
        String str3="zdz";
        System.out.println(str2==str3);//true why?请听下回对象与内存分析

        System.out.println("=========================");

        //布尔拓展
        boolean flag=true;
        if (flag==true){}//菜鸟
        if (flag){}//老鸟
        //less is more! 代码要精简
    }

 

java类型转换
Java是强类型语言,有些运算需要进行类型转换
byte,short,char->int->long->float->double
运算中,不同类型的数据先转化为同一类型,然后进行运算

推荐阅读