1. 整型
整型有三張表示方式:
- 10進(jìn)制 :int i = 10;
- 8進(jìn)制 :int i = 013;
- 16進(jìn)制 :int i = 0xaF;
2. 字符型
字符型有四種表示方式:
- 單個字符 : char c = ‘a(chǎn)’;
- 轉(zhuǎn)義字符 : char c = ‘\n’;
- unicode編碼: char c = ‘\u9999’;
- int : char c = 97;
3. 浮點(diǎn)型
浮點(diǎn)型有兩種表示方式:
- 10進(jìn)制數(shù) : 5.123
- 科學(xué)計數(shù)法 : 5.123E2
public class FloatTest {
public static void main(String[] args)
{
//浮點(diǎn)型數(shù)據(jù)必須包含小數(shù)點(diǎn),否則會當(dāng)作int處理
//浮點(diǎn)型默認(rèn)是double型的,默認(rèn)是64位的,所以定義float類型是,必須在最后加f表示float型
float f = 5.0f;
float af = 5.123456789f;
double df = 5.12345678987654321;
//浮點(diǎn)數(shù)是使用二進(jìn)制數(shù)據(jù)的科學(xué)計數(shù)法表示,所以不可能精確表示一個浮點(diǎn)數(shù).下面將看到的值已經(jīng)發(fā)生了改變
System.out.println("af="+af);
System.out.println("df="+df);
//result:af=5.123457,df=5.123456789876543
double a = 0.0;
double c = Double.NEGATIVE_INFINITY;
float d = Float.NEGATIVE_INFINITY;
//看到float和double的負(fù)無窮大的是相等的。
System.out.println(c == d);
//0.0除以0.0將出現(xiàn)非數(shù)
System.out.println(a / a);
//兩個非數(shù)之間是不相等的
System.out.println(a / a == Float.NaN);
//所有正無窮大都是相等的
System.out.println(6.0 / 0 == 555.0/0);
//負(fù)數(shù)除以0.0得到負(fù)無窮大
System.out.println(-8 / a);
System.out.println(0.0 / 0.0);
//result:NaN
//下面代碼將拋出除以0的異常
System.out.println(0 / 0);
}
}
4. 布爾型
只能是"true"或"false",不能和其他基本數(shù)據(jù)類型相轉(zhuǎn)換
5. 類型轉(zhuǎn)換
a. 自動類型轉(zhuǎn)換
數(shù)據(jù)范圍小的變量或數(shù)值可以直接賦給數(shù)據(jù)范圍大的變量或數(shù)值,系統(tǒng)可以自動轉(zhuǎn)換
b. 強(qiáng)制類型轉(zhuǎn)換
類似將一個大瓶子的水倒入一個小瓶子中,如果大瓶子中的水過多,將可能導(dǎo)致溢出.
代碼清單:強(qiáng)制類型轉(zhuǎn)換
public class Conversion {
/**
* @param args
*/
public static void main(String[] args) {
/*
* int是32位的,例如233:
* 0000 0000 0000 0000 0000 0000 1110 1001
* 將int賦給byte,byte是8位的,所以會將前面的24截取掉,變成了
* 1110 1001
* 整數(shù)在計算機(jī)中是以補(bǔ)碼的形式保存的,正數(shù)的補(bǔ)碼和原碼相同,負(fù)數(shù)的補(bǔ)碼是原碼取反加1,最高位不變.所以
* 11101001
* | 減1,成反碼
* 11101000
* | 取反成原碼
* 10010111 也就是-23
*/
int intValue = 233;
byte byteValue = (byte)intValue;
System.out.println(byteValue);
double doubleValue = 3.98;
int intValue1 = (int)doubleValue;
System.out.println(intValue1);
}
}