關於資料型別,以及一些拓展內容
阿新 • • 發佈:2021-10-03
整數型別
-
byte:佔1個位元組 範圍:-128-127
-
short:佔2個位元組 範圍:-32768-32767
-
int:佔4個位元組 範圍:-2147483648-2147483647
-
long:佔8個位元組 範圍:很大
1 //整數拓展: 進位制 二進位制0b 十進位制 八進位制0 十六進位制0x 2 3 int i = 10; 4 int i2 = 010;//八進位制0 5 int i3 = 0x10;//十六進位制0x 0~9 A~F 16 6 7 System.out.println(i);//10 8 System.out.println(i2);//8 9 System.out.println(i3);//16
浮點型別
-
float:佔4個位元組
-
1 //浮點數拓展:銀行業務怎麼表示?錢 2 //BigDecimal 數學工具類 3 //================================================== 4 //float 特點:有限 離散 舍入誤差 大約 接近但不等於 5 //double 6//最好 避免 使用浮點數進行比較 7 //最好 避免 使用浮點數進行比較 8 9 float f = 0.1f;//0.1 10 double d = 1.0/10;//0.1 11 12 System.out.println(f==d);//false 13 System.out.println(f); 14 System.out.println(d); 15 16 float d1 = 232323232323231f; 17 floatd2 = d1 + 1; 18 19 System.out.println(d1==d2);//true
字元型別
-
char:佔2個位元組
1 //字元拓展 2 char c1 = 'a'; 3 char c2 = '中'; 4 5 System.out.println(c1); 6 System.out.println((int)c1);//強制型別轉換 97 7 8 System.out.println(c2); 9 System.out.println((int)c2);//20013 10 11 //所有的字元本質還是數字 12 //編碼 Unicode 表:(97 = a 65 = A) 2位元組 0 - 65536
boolean型別
佔1位,其值只有true和false兩個