Java 基础-数据类型

public static void main(String[] args) {
        //测试整型变量
        int  a = 15;
        int  b = 015;    //以0开头是八进制
        int  c = 0x15;    //以0x开头是16进制
        int  d = 0b1101;    //以0b开头是二进制

        
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        
        byte  age = 30;
        short  salary = 30000;
        int   population = 2000000000;            //整型常量默认是int类型
        long  globalPopulation = 7400000000L;    //后面加L就是这是一个long类型的常量
        
        
    }

public static void main(String[] args) {
        float a = 3.14F;
        double b = 6.28;
        double c = 628E-2;

        System.out.println(c);

        // 浮点数是不精确的,一定不要用于比较!
        float f = 0.1f;
        double d = 1.0 / 10;
        System.out.println(f == d); // 结果为false

        float d1 = 423432423f;
        float d2 = d1 + 1;
        if (d1 == d2) {
            System.out.println("d1==d2");// 输出结果为d1==d2
        } else {
            System.out.println("d1!=d2");
        }

        System.out.println("##################");    
        //使用精确浮点运行,推荐:BigDecimal
        BigDecimal bd = BigDecimal.valueOf(1.0);
        bd = bd.subtract(BigDecimal.valueOf(0.1));
        bd = bd.subtract(BigDecimal.valueOf(0.1));
        bd = bd.subtract(BigDecimal.valueOf(0.1));
        bd = bd.subtract(BigDecimal.valueOf(0.1));
        bd = bd.subtract(BigDecimal.valueOf(0.1));
        System.out.println(bd);// 0.5
        System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);// 0.5000000000000001
        
        BigDecimal  bd2 = BigDecimal.valueOf(0.1);
        BigDecimal  bd3 = BigDecimal.valueOf(1.0/10.0);
        
        System.out.println(bd2.equals(bd3)); 

    }

 

 

public static void main(String[] args) {
        char  a = 'T';
        char  b = '尚';
        char c = '\u0061';
        System.out.println(c);
        
        //转义字符
        System.out.println(""+'a'+'\n'+'b'); 
        System.out.println(""+'a'+'\t'+'b'); 
        System.out.println(""+'a'+'\''+'b');        //a'b 

        
        
        //String就是字符序列
        String  d = "abc";
        
        //测试布尔类型
        boolean  man = false;
        
        if(man){        //极端不推荐:man==true
            System.out.println("男性");
        }

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值