Java300集(二)java基本数据类型

Java是一种强类型语言,每个变量都必须声明其类型。

数值型各类型范围:

整数类型:

public class TestDataType {
    public static void main(String[] args) {
        //浮点数常量默认类型是double
        //整数的另外三种表现形式:
        int a = 10;
        System.out.println("source data:"+a);
        System.out.println("转换成二进制:"+Integer.toBinaryString(a));
        System.out.println("转换成八进制:"+Integer.toOctalString(a));
        System.out.println("转换成十六进制:"+Integer.toHexString(a));
        //如果数据的大小没有超过byte/short/char的表述范围那么可以自动转型,否则报错
        byte b = 100;
        System.out.println("byte结果为:"+b);

        //对于long类型的转型,后面加L
        long l = 120003003L;
        System.out.println("long结果为:"+l);
    }
}

浮点类型:

public class TestDataTypeForFloat {
    public static void main(String[] args) {

        //浮点数常量默认类型是double
        double d = 3.14;
        System.out.println("double结果:" + d);

        float f = 3.14F;
        System.out.println("float结果:" + f);

        //科学计数法
        double d1 = 314e-2;
        System.out.println("科学计数法结果:" + d1);

        /**
         * 浮点数存在舍入误差,很多数字不能精确表示。
         * 如果需要进行不产生舍入误差的精确数字计算,需要使用BigDecimal
         *
         */

        float f1 = 0.1f;
        double d2 = 1.0 / 10;
        System.out.println("两者比较结果:" + (f1 == d2));//false

    }
}

字符型和布尔类型:

public class TestDataTypeForChatAndBoolean {
    public static void main(String[] args) {
        char a1 = 'a';
        char a2 = '上'; //unicode 0-65535
        System.out.println("a1=" + a1);
        System.out.println("a2=" + a2);
        //转义字符' " \ \t \n
        char a3 = '\'';
        System.out.println("a3=" + a3);

        char a4 = 'a';
        int i = a4 + 2;//ASCII码
        System.out.println("i=" + i);

        char a5 = (char) i;//int四个字节,char两个字节,强制转换
        System.out.println("a5=" + a5);

        //循环打印a-z
        for (int j = 0; j < 26; j++) {
            char temp = (char) (a4 + j);
            System.out.print(temp + ",");
        }
        System.out.println();


        //布尔类型占了一位,而非一个字节
        boolean b = true;
        if (b) {
            System.out.println("true");
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值