Java中的基礎(chǔ)類型有:byte、short、int、long、float、double、char和boolean。
它們可被分為四種類型,整型、浮點型、char型和boolean型。
整型:byte、short、int、long 分別占用1、2、4、8個字節(jié)的空間;
浮點型:float、double 分別占用4、8個字節(jié);
char型:char 占用2個字節(jié);
boolean型:boolean 占用1位。
在Java中不能直接使用二進制表示數(shù)字,可以使用8進制或者16進制來間接表示。這些數(shù)字類型在計算機中是如何表示的哪?通過下面的代碼大家可以看個大概,具體為什么會這樣大家可以看看我博客上“計算機中如何表示數(shù)字”的那幾篇文章,其實這段代碼主要是為了配合那幾篇文章而來的。
至于char型在后面要單獨介紹,因為Java中的char型是很獨特的,它會占用兩個字節(jié)的空間。boolean就用不著介紹了,但要記住和c不同,Java中的boolean型和整數(shù)沒有關(guān)系,它們之間無法進行轉(zhuǎn)換。
public class PrimitiveType {
public static void main(String[] args){
//java中整型數(shù)計算采用補碼,可以通過十六進制或者八進制形式對整型數(shù)直接賦值,java中不能
//直接使用二進制數(shù)
//byte
byte maxByte=0x7f;
System.out.println("最大byte:0x7f 真值:"+maxByte);
byte minByte=(byte) 0x80;
System.out.println("最小byte:0x80 真值:"+minByte);
//short
short maxShort=0x7fff;
System.out.println("最大short:0x7fff 真值:"+maxShort);
short minShort=(short) 0x8000;
System.out.println("最小short:0x8000 真值:"+minShort);
//int
int maxInt=0x7fffffff;
System.out.println("最大int:0x7fffffff 真值:"+maxInt);
int minInt=0x80000000;
System.out.println("最小int:0x80000000 真值:"+minInt);
//long
System.out.println("最大long:0x7fffffffffffffff 真值:"+
Long.decode("0x7fffffffffffffff"));
System.out.println("最小long:0x8000000000000000 真值:"+
Long.decode("-0x8000000000000000"));
//算是一個bug吧,補碼表示已經(jīng)是帶符號的數(shù)字了
//java中對小數(shù)直接量默認為double類型,定義float型需要在小數(shù)后面加上字母F,大小寫不限
float f=0.1F;
double d=0.1D;//定義double,在小數(shù)后面也可以加D,大小寫不限
//---------------------Java中浮點數(shù)計算都遵循IEEE754規(guī)范-------------------------//
//與整型數(shù)不同,浮點數(shù)在java中無法直接通過十六進制或者八進制形式定義
//最大正數(shù)(0 11111110 11111111111111111111111)=(0x7f7fffff)
float maxPositiveNumber=Float.intBitsToFloat(0x7f7fffff);
System.out.println("最大正單精度浮點數(shù):0x7f7fffff 真值:"+maxPositiveNumber);
//最小正數(shù)(0 00000001 00000000000000000000000)=(0x00800000)
float minPositiveNumber=Float.intBitsToFloat(0x00800000);
System.out.println("最小正單精度浮點數(shù):0x00800000 真值:"+minPositiveNumber);
//0,IEEE754 規(guī)定 E=0 M=0,則值為0,(0 00000000 00000000000000000000000)=(0x00000000)
float positoveZero=Float.intBitsToFloat(0x00000000);
System.out.println("正零:"+positoveZero);
float negativeZero=Float.intBitsToFloat(0x80000000);
System.out.println("負零:"+negativeZero);
//最大負數(shù)(1 00000001 00000000000000000000000)=(0x80800000)
float maxNegativeNumber=Float.intBitsToFloat(0x80800000);
System.out.println("最大負單精度浮點數(shù):0x80800000 真值:"+maxNegativeNumber);
//最小負數(shù)(1 11111110 11111111111111111111111)=(0xff7fffff)
float minNegativeNumber=Float.intBitsToFloat(0xff7fffff);
System.out.println("最小負單精度浮點數(shù):0xff7fffff 真值:"+minNegativeNumber);
//NaN,非數(shù)值,JDK API doc 上面也有提到
System.out.println("NaN:E=255 M<>0 例如:0x3f800001 運行結(jié)果:"+
Float.intBitsToFloat(0x7f800001));
//POSITIVE_INFINITY,正無窮大
System.out.println("POSITIVE_INFINITY:S=0 E=255 M=0 0x7f800000 運行結(jié)果:"+
Float.intBitsToFloat(0x7f800000));
//NEGATIVE_INFINITY,負無窮大
System.out.println("NEGATIVE_INFINITY:S=0 E=255 M=0 0xff800000 運行結(jié)果:"+
Float.intBitsToFloat(0xff800000));
//雙精度浮點數(shù)同理可得
//下面對char型進行討論
char[] codeUnits;
//基本多語言級別 basic multilingual plane
//ASC2 code
for(int i = 0 ; i < 128 ;i++){
codeUnits = Character.toChars(i);
if( codeUnits.length == 1 ){
System.out.println(i+" "+(char)i);
}
}
//漢字 '嚴'
char yan = '/u4e25';
codeUnits = Character.toChars(0x4e25);
System.out.printf("漢字:"+yan+" 代碼點所占代碼單元長度"+codeUnits.length+
" (0x%x) ,它屬于基本多語言級別。/n",(int)codeUnits[0]);
//輔助字符 supplementary character
//代碼點0x1d56b
codeUnits = Character.toChars(0x1d56b);
//判斷代碼單元的高低位
System.out.printf("代碼點0x%x 在UTF-16表示中被分解為兩個代碼單元 0x%x 0x%x /n",
0x105600,(int)codeUnits[0],(int)codeUnits[1]);
System.out.printf("0x%x is HighSurrogate:"+Character.isHighSurrogate(codeUnits[0])+
"/n",(int)codeUnits[0]);
System.out.printf("0x%x is HighSurrogate:"+Character.isHighSurrogate(codeUnits[1])+
"/n",(int)codeUnits[1]);
System.out.printf("0x%x is LowSurrogate:"+Character.isLowSurrogate(codeUnits[0])+"/n",
(int)codeUnits[0]);
System.out.printf("0x%x is LowSurrogate:"+Character.isLowSurrogate(codeUnits[1])+"/n",
(int)codeUnits[1]);
}
}