本文源碼: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