生活随笔
收集整理的這篇文章主要介紹了
自动装箱/自动拆箱
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前提:為什么要引入包裝類?
為了使基本數據類型的變量具有類的特征
8種基本數據類型對應的包裝類:
整數類型:byte(8位),short(16位),int(32位),long(64位)
包裝類:Byte,Short,Integer,Long
浮點類型:float(單精度32位),double(雙精度64位)。
包裝類:Float,Double
字符類型:char(16位UNICODE字符)
包裝類Character
布爾類型:boolean
包裝類:Boolean
jdk5.0之后提供了自動裝箱和自動拆箱:
int num2
= 10;Integer in1
= num2
; boolean b1
= true;Boolean b2
= b1
;System
.out
.println("自動裝箱測試:"+ in1
);System
.out
.println("自動裝箱測試:"+ b2
);
int num3
= in1
;
System
.out
.println("自動拆箱測試:"+ num3
);
基本數據類型/包裝類與String類型之間的轉換:
基本數據類型/包裝類–>String: String.valueOf(Xxx xxx)
String–>基本數據類型/包裝類: parseXxx(String s)
tips:要轉成String,找String類的方法
要轉成包裝類,找包裝類里的方法
float f1
= 12.3f;String str2
= String
.valueOf(f1
); Double d1
= new Double(12.4);String str3
= String
.valueOf(d1
); System
.out
.println("str2:" + str2
);System
.out
.println("str3:" + str3
);
String str2
= "true";
String str3
= "true132";
boolean b1
= Boolean
.parseBoolean(str2
);
boolean b2
= Boolean
.parseBoolean(str3
);
System
.out
.println(b1
);
System
.out
.println(b2
);
tips:
①. Vector類中關于添加元素,只定義了形參為object類型的方法:
②. v.addElement(Object obj); //基 本數據類型–>包裝類–>使用多態
總結
以上是生活随笔為你收集整理的自动装箱/自动拆箱的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。