整型和布林型及部分進位制
阿新 • • 發佈:2022-01-20
整型
型別 | 佔用儲存空間 | 表數範圍 |
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); } }