Java包裝類之自動裝箱與拆箱
JDK 5.0之前
基本數(shù)據(jù)類型<---->包裝類:調(diào)用包裝類的構(gòu)造器(代碼里有知識點和注意點)
轉(zhuǎn)換目的:有了類的特點,就可以調(diào)用類中的方法
public class WrapperTest {
// 基本數(shù)據(jù)類型--->包裝類:調(diào)用包裝類的構(gòu)造器
@Test
public void test1() {
int num1 = 10;
Integer in1 = new Integer(num1);
System.out.println(in1.toString());//10
Integer in2 = new Integer("123");
System.out.println(in2.toString());//123
// 報異常:NumberFormatException
Integer in3 = new Integer("123abc");//調(diào)用Integer必須全是數(shù)字
System.out.println(in3.toString());
//有點特殊:因為底層源碼 return ((s != null) && s.equalsIgnoreCase("true"));
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean("True");//true
System.out.println(b2);
Boolean b3 = new Boolean("true123");//flase
Order order = new Order();
System.out.println(order.isMale);//false//因為基本數(shù)據(jù)類型的默認(rèn)初始化值為:false
System.out.println(order.isFamale);//null//此時為引用數(shù)據(jù)類型(類),默認(rèn)初始化值:null
}
}
class Order {
boolean isMale;
Boolean isFamale;
}

包裝類--->基本數(shù)據(jù)類型:調(diào)用包裝類的xxxValue()
轉(zhuǎn)換目的:就可以進行加減乘除的運算
public class WrapperTest {
@Test
public void test2() {
Integer in1 = new Integer(12);
int i1 = in1.intValue();
System.out.println(i1 + 1);//13
Float f1 = new Float(12.3);
float f2 = f1.floatValue();
System.out.println(f2 + 1);//13.3
}
}
JDK5.0之后
自動裝箱:本質(zhì)是替換之前的調(diào)用包裝類的構(gòu)造器
自動拆箱:本質(zhì)是替換之前的調(diào)用包裝類的xxxValue()
public class WrapperTest {
@Test
public void test3() {
//自動裝箱
int num2 = 10;
Integer in1 = num2;
boolean b1 = true;
Boolean b2 = b1;
//自動拆箱
System.out.println(in1.toString());
int num3 = in1;
}
}
基本數(shù)據(jù)類型、包裝類---->String類型:調(diào)用String重載的valueOf(Xxx xxx)
public class WrapperTest {
@Test
public void test4() {
//方式一:連接運算
int num1 = 10;
String str1 = num1 + "";
//方式二:調(diào)用String的valueOf(Xxx xxx)
float f1 = 12.3f;
String str2 = String.valueOf(f1);
Double d1 = new Double(12.4);
String str3 = String.valueOf(d1);
System.out.println(str2);//"12.3"
System.out.println(str3);//"12.4"
}
}
注意點:“+”運算符是最簡單、最快捷,也是使用最多的字符串連接方式。在使用"+"運算符連接字符串和int型(或double型)數(shù)據(jù)時,“+”將int(或double)型數(shù)據(jù)自動轉(zhuǎn)成String類型。
String--->基本數(shù)據(jù)類型、包裝類:調(diào)用包裝類的parseXxx(String s)
public class WrapperTest {
@Test
public void test5() {
String str1 = "123";
int num2 = Integer.parseInt(str1);//若為"123a",則會報NumberFormatException
System.out.println(num2 + 1);
String str2 = "true";
boolean b1 = Boolean.parseBoolean(str2);//非true,即flase
System.out.println(b1);
}
注意:轉(zhuǎn)換時,可能會報NumberFormatException
到此這篇關(guān)于Java包裝類之自動裝箱與拆箱的文章就介紹到這了,更多相關(guān)Java 裝箱 拆箱內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
RestTemplate發(fā)送請求時Cookie的影響及注意事項說明
這篇文章主要介紹了RestTemplate發(fā)送請求時Cookie的影響及注意事項說明,具有很好的參考價值,希望對大家有所幫助。2023-07-07
Spring Profile與PropertyPlaceholderConfigurer項目多環(huán)境配置切換
這篇文章主要介紹了Spring Profile與PropertyPlaceholderConfigurer項目多環(huán)境配置切換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
java返回前端樹形結(jié)構(gòu)數(shù)據(jù)的2種實現(xiàn)方式
近期項目有個需求,需要將組織機構(gòu)數(shù)據(jù)拼成樹型結(jié)構(gòu)返回至前端,下面這篇文章主要給大家介紹了關(guān)于java返回前端樹形結(jié)構(gòu)數(shù)據(jù)的2種實現(xiàn)方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
JAVA根據(jù)ip地址獲取歸屬地的實現(xiàn)方法
本文主要介紹了JAVA根據(jù)ip地址獲取歸屬地的實現(xiàn)方法,要通過Java程序獲取IP地址對應(yīng)的城市,需要借助第三方的IP地址庫,下面就來介紹一下,感興趣的可以了解一下2023-10-10
Java中的ScheduledThreadPoolExecutor定時任務(wù)詳解
這篇文章主要介紹了Java中的ScheduledThreadPoolExecutor詳解,??ScheduledThreadPoolExecutor?繼承自?ThreadPoolExecutor,它主要用來在給定的延遲之后運行任務(wù),或者定期執(zhí)行任務(wù),ScheduledThreadPoolExecutor?的功能與?Timer?類似<BR>,需要的朋友可以參考下2023-12-12
Java中HashMap和Hashtable及HashSet的區(qū)別
以下是對Java中HashMap和Hashtable及HashSet的區(qū)別進行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-09-09

