整型和布尔型及部分进制

整型

类型 占用存储空间 表数范围
byte 1字节  - 27 ~ 27- 1(-128 ~ 127)
short 2字节 -215 ~ 215 - 1(-32768 ~ 32767)
int 4字节 -231 ~ 231 -  1(约21亿)
long 8字节 -263 ~ 263 - 1 (9 * 1018左右)
public class Test{
    public static void main(String[] args){
        int salary = 300000; // 整型常量默认为int型
        //int yearSalary = 3000000000; 会报错超出了21亿的范围
        long yearSalary = 3000000000L; // 把整型常量定义为long型
        //long yearSalary = 3000000000 也会报错
        System.out.println("月薪" + salary);
        System.out.println("某个津巴布韦居民的薪水" + yearSalary + "津巴布韦币");

        byte  a = 50;
        short b = 300;
        System.out.println(a);
        System.out.println(b);
    }
}

 

 

几种表示形式

1.十进制整数, 正常表示 如:99、66、-321

2.八进制整数,要求以0开头,如:015

3.十六进制整数,要求以 0x 或 0X 开头,如0x15

4.二进制整数,要求以 0b 或 0B 开头, 如 0b01010101

public class Test{
    public static void main(String[] args){
        int a = 100; // 十进制
        int b = 014; // 八进制
        int c = 0xff; // 十六进制
        int d = 0b01010101; // 二进制
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}

 

 

 

布尔型

1.boolean只有两个值, true 和 false

2.在内存中占一个字节(boolean数组时)或四个字节(单个bookean)

3. 不可以使用0和非0的整数替代true和false,这点和C语言不同

public class Test{
    public static void main(String[] args){
        boolean b1 = true;
        boolean b2 = false;
        System.out.println("b1是" + b1);
        System.out.println("b2是" + b2);
    }
}

posted @ 2022-01-20 16:24  我就一水  阅读(225)  评论(0)    收藏  举报