麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

Java 每天學習一點點之數據類型

2019-11-14 08:43:45
字體:
來源:轉載
供稿:網友

特別聲明:如下內容均來自于不同網絡課程視頻或書籍,整理合并完成。 如果有整理錯誤,忘及時指出,共同進步,不勝感激。

java 入門之數據類型

Java 數據類型分為兩種

1. 基本數據類型

數值型(兩種類型):

??整數類型: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}

??浮點類型: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 給字符型變量賦值時,我們使用單引號,而非雙引號,否則出現錯誤。 例如:

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 該類型賦值結果只有 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. 如何正確選取合理的數據類型?

?? a). 符合實際情況 ??? 例如:統計記錄學生身高時,我們應該選擇float 類型,即小數類型,而非整數類型。

?? b). 事先考慮需要賦值的大小作用范圍 ??? 例如:計算某地區人口數量,假設選擇 short 類型,則造成數據溢出導致錯誤

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. 數據精度問題? ??? 例如:當把小數3.14 賦值給 int 型變量時,我們將損失精度,因為 int 為整數型,只會記錄3損失小數部分。 若將低精度數值例如整數4 賦值給 ??? double,則不會造成精度損失。

?? 3. float 賦值問題? ??? 例如:float賦值時需要在結尾加上“f” 以表示該類型為float,否則系統將會默認為double 類型。

?? 4. 不同數據類型之間的計算問題? ??? 例如:int 型的數值10除以double 型數值2.5,則結果為4.0 與double 類型一致。

?? 5. 在輸出語句進行計算問題? ??? 例如:

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}

??? 我們可以看見第一條輸出結果被轉化成了字符串string格式,因此當需要在輸出語句中進行計算式,務必加上括號。

?? 6. 數據可以進行強制轉換 ??? 例如:

public static void main(String[] args) { int x = 10; float y = (float) x; System.out.println(y); output: 10.0}

2. 引用數據類型

類: 接口: 數組:

未完待續。。。 更新時間 每周一,三,五,日。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 97色在线观看免费视频 | 亚洲爱爱图 | 成人免费一区 | 亚洲国产视频在线 | 久草在线手机视频 | 色女生影院 | 亚洲二区免费 | 国产91精品久久久久久久 | 免费一及片 | 亚洲电影免费观看国语版 | 国产资源在线播放 | 精品国产精品久久 | 久久99国产精品视频 | 亚洲精中文字幕二区三区 | 久久99国产精品免费网站 | 欧美精品免费一区二区三区 | 久久国产精品二区 | 在线成人免费观看视频 | 国产午夜三级一区二区三桃花影视 | 免费看污视频在线观看 | 麻豆视频在线观看免费网站 | 少妇一级淫片高潮流水电影 | 国产精品影视 | 99久久婷婷国产综合精品青牛牛 | 久久久精品视 | 国产免费区 | 久久久在线免费观看 | 国产91成人 | 久久久久久久久久久久网站 | 成人在线视频网 | 毛片电影在线看 | 欧美成人午夜影院 | 激情亚洲一区二区 | 久久91精品久久久久清纯 | 国产成年人在线观看 | 午夜噜噜噜 | 国产啊v在线观看 | 成人在线视频精品 | 五月天影院,久久综合, | 精品中文字幕在线观看 | 免费放黄网站在线播放 |