特別聲明:如下內(nèi)容均來(lái)自于不同網(wǎng)絡(luò)課程視頻或書(shū)籍,整理合并完成。 如果有整理錯(cuò)誤,忘及時(shí)指出,共同進(jìn)步,不勝感激。
??整數(shù)類(lèi)型:byte,short,int,long (從小到大順序排列)
??取值范圍:
???? -byte:
public static void main(String[] args) { byte x = Byte.MIN_VALUE; byte y = Byte.MAX_VALUE; System.out.???? - short: public static void main(String[] args) { short x = Short.MAX_VALUE; short y = Short.MIN_VALUE; System.out.println("The range of short type between "+x+" and "+ y); output: The range of short type between -32768 and 32767 }???? - int:
public static void main(String[] args) { int x = Integer.MAX_VALUE; int y = Integer.MIN_VALUE; System.out.println("The range of int type between "+x+" and "+ y); output: The range of int type between -2147483648 and 2147483647}???? - long:
public static void main(String[] args) { long x = Long.MAX_VALUE; long y = Long.MIN_VALUE; System.out.println("The range of long type between "+x+" and "+ y); out put: The range of long type between -9223372036854775808 and 9223372036854775807}??浮點(diǎn)類(lèi)型:float,double (從小到大順序排列) ??取值范圍:
???? -float:
public static void main(String[] args) { float x = Float.MAX_VALUE; float y = Float.MIN_VALUE; System.out.println("The range of float type between "+x+" and "+y); output:The range of float type between -3.4028235E38 and 1.4E-45}???? -double:
public static void main(String[] args) { double x = Double.MAX_VALUE; double y = Double.MIN_VALUE; System.out.println("The range of double type between "+x+" and "+y); output: The range of double type between -1.7976931348623157E308 and 4.9E-324}字符型:char 給字符型變量賦值時(shí),我們使用單引號(hào),而非雙引號(hào),否則出現(xiàn)錯(cuò)誤。 例如:
public static void main(String[] args) {char x = 'a';System.out.println("The value of a is "+x);output: The value of x is a}public static void main(String[] args) {char x = "b"; System.out.println("The value of a is "+b); output: Exception in thread "main" java.lang.Error: Unresolved compilation problems: Type mismatch: cannot convert from String to charb cannot be resolved to a variable.}布爾型:boolean 該類(lèi)型賦值結(jié)果只有 TRUE 和 False 兩種。 例如:
public static void main(String[] args) {boolean x = true;boolean y = false;System.out.println(x+" and "+y); output: true and false }??特別注意:
?? 1. 如何正確選取合理的數(shù)據(jù)類(lèi)型?
?? a). 符合實(shí)際情況 ??? 例如:統(tǒng)計(jì)記錄學(xué)生身高時(shí),我們應(yīng)該選擇float 類(lèi)型,即小數(shù)類(lèi)型,而非整數(shù)類(lèi)型。
?? b). 事先考慮需要賦值的大小作用范圍 ??? 例如:計(jì)算某地區(qū)人口數(shù)量,假設(shè)選擇 short 類(lèi)型,則造成數(shù)據(jù)溢出導(dǎo)致錯(cuò)誤
public static void main(String[] args) { short x = 20000000; System.out.println(x); output: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from int to short}?? 2. 數(shù)據(jù)精度問(wèn)題? ??? 例如:當(dāng)把小數(shù)3.14 賦值給 int 型變量時(shí),我們將損失精度,因?yàn)?int 為整數(shù)型,只會(huì)記錄3損失小數(shù)部分。 若將低精度數(shù)值例如整數(shù)4 賦值給 ??? double,則不會(huì)造成精度損失。
?? 3. float 賦值問(wèn)題? ??? 例如:float賦值時(shí)需要在結(jié)尾加上“f” 以表示該類(lèi)型為float,否則系統(tǒng)將會(huì)默認(rèn)為double 類(lèi)型。
?? 4. 不同數(shù)據(jù)類(lèi)型之間的計(jì)算問(wèn)題? ??? 例如:int 型的數(shù)值10除以double 型數(shù)值2.5,則結(jié)果為4.0 與double 類(lèi)型一致。
?? 5. 在輸出語(yǔ)句進(jìn)行計(jì)算問(wèn)題? ??? 例如:
public static void main(String[] args) { int x = 10; int y =20; System.out.println("This is wrong: "+x+y); output: 1020 System.out.println("This is correct"+(x+y)); output:30}??? 我們可以看見(jiàn)第一條輸出結(jié)果被轉(zhuǎn)化成了字符串string格式,因此當(dāng)需要在輸出語(yǔ)句中進(jìn)行計(jì)算式,務(wù)必加上括號(hào)。
?? 6. 數(shù)據(jù)可以進(jìn)行強(qiáng)制轉(zhuǎn)換 ??? 例如:
public static void main(String[] args) { int x = 10; float y = (float) x; System.out.println(y); output: 10.0}未完待續(xù)。。。 更新時(shí)間 每周一,三,五,日。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注