本文源碼: GitHub·點這裡 || GitEE·點這裡
一、基本類型
1、基本類型
不使用New建立,聲明一個非引用傳遞的變量,且變量的值直接置于堆棧中,大小不随運作環境變化,效率更高。使用new建立的引用對象存儲在堆中。
2、基本資訊
基本類型包括如下幾種:byte、short、int、long、float、double、boolean、char,可以通過相關方法檢視範圍大小。
public class IntType01 {
public static void main(String[] args) {
System.out.println("進制位數:"+Integer.SIZE);
System.out.println("最小值:"+Integer.MIN_VALUE);
System.out.println("最大值:"+Integer.MAX_VALUE);
System.out.println("進制位數:"+Double.SIZE);
System.out.println("最小值:"+Double.MIN_VALUE);
System.out.println("最大值:"+Double.MAX_VALUE);
}
}
二、案例用法
1、類型轉換
自動轉換
:範圍小的資料類型可以自動轉換成範圍大的資料類型。
強制轉換
:把一種資料類型轉換為另外一種資料類型。
類型提升
:表達式運算中有不同的資料類型,類型會自動向範圍大的提升。
public class IntType02 {
public static void main(String[] args) {
// 自動轉換
int i = 112 ;
long j = i ;
System.out.println(j);
// 強制轉換
double d = 13.14 ;
int f = (int)d;
System.out.println(f);
// 類型提升
long r = i * j ;
System.out.println(r);
}
}
注意:類型轉換中最需要關注的問題就是範圍大小問題。
2、包裝器類型
基本資料類型不符合面向對象思想,進而出現了包裝器類型, 并且包裝器添加了更多的屬性和方法,自動包裝功能可以将基本類型轉換為包裝器類型。Java為每個原始類型都提供了一個封裝類,Integer、Double、Long、Boolean、Byte等等。
public class IntType03 {
public static void main(String[] args) {
Integer int1 = null ;
Double dou1 = 13.14 ;
Long lon1 = 123L ;
}
}
Integer變量的預設值為null,說明Integer可以區分出未指派和值為0的差別,好比考試得0分和沒參加考試的差別。
3、字元類型
char類型變量是用來儲存Unicode編碼的字元的,unicode字元集包含漢字。
public class IntType04 {
public static void main(String[] args) {
char cha1 = '知';
System.out.println(cha1);
}
}
注意
:可能存在特殊生僻字沒有包含在unicode編碼字元集中。
4、指派和運算
+= 和 =
的區分:
short s1=1;s1=s1+1與short s1=1;s1+=1;
問題。
public class IntType05 {
public static void main(String[] args) {
short s1 = 1 ;
// s1 = s1 + 1 ; // 變異錯誤:s1自動向int類型轉換
s1 += 1 ;
System.out.println(s1);
}
}
+=
運算符是java語言規定的,編譯器會對它進行識别處理,是以可以正确編譯。
5、布爾類型
兩個邏輯值:
true
和
false
,通常用來表示關系運算的結果。
public class IntType06 {
public static void main(String[] args) {
// 存在精度損失問題:0.30000000000000004
System.out.println(3*0.1);
// true
System.out.println(0.3 == 0.3);
// false
System.out.println(3*0.1 == 0.3);
}
}
三、Float和Dubble
1、基礎概念
這兩個類型可能大部分情況下都說不明白關系和區分,首先要了解幾個基礎概念。
浮點數
:在計算機中用以近似表示任意某個實數。具體的說,這個實數由一個整數或定點數乘以某個基數(計算機中通常是2)的整數次幂得到
單精度浮點數
:單精度浮點數是用來表示帶有小數部分的實數,一般用于科學計算。占用4個位元組(32位)存儲空間
雙精度浮點數
:雙精度浮點數(double)是計算機使用的一種資料類型,使用64位(8位元組)來存儲一個浮點數。
2、對比分析
- Float基本描述
位數:32
最小值:1.4E-45
最大值:3.4028235E38
- Double基本描述
位數:64
最小值:4.9E-324
最大值:1.7976931348623157E308
- 案例描述
float和double聲明和轉換相關示範案例。
public class IntType07 {
public static void main(String[] args) {
// float 聲明
float f1 = 12.3f ;
// double 聲明
double d1 = 13.4 ;
// 向下轉型,需要強制轉換
float f2 = (float) d1 ;
System.out.println("f1="+f1+";d1="+d1+";f2="+f2);
}
}
四、高精度類型
1、BigInteger
支援任意大小的整數運算,且不會再運算過程有任何丢失情況,沒有對應的基本類型,運算也會變得相對複雜,運算速度自然也就會下降。
2、BigDecimal
支援任意精度的定點數,通常用來進行精确的貨币計算,在公司的日常開發中,這裡通常是硬性要求。
public class IntType08 {
public static void main(String[] args) {
BigDecimal dec1 = new BigDecimal(3.0) ;
BigDecimal dec2 = new BigDecimal(2.11) ;
// 精确加法運算
BigDecimal res1 = dec1.add(dec2) ;
System.out.println(res1);
// 精确減法運算,并截取結果
// HALF_UP:四舍五入
BigDecimal res2 = dec1.subtract(dec2);
System.out.println(res2.setScale(1, RoundingMode.HALF_UP));
// 精确乘法運算
BigDecimal res3 = dec1.multiply(dec2) ;
System.out.println(res3.doubleValue());
// 精确除法運算,并截取結果
// ROUND_DOWN:直接按保留位數截取
BigDecimal res4 = dec1.divide(dec2,2,BigDecimal.ROUND_DOWN);
System.out.println(res4);
}
}
五、源代碼位址
GitHub·位址
https://github.com/cicadasmile/java-base-parent
GitEE·位址
https://gitee.com/cicadasmile/java-base-parent