一.裝箱拆箱
1.所有的基本類型都有對應(yīng)的類類型(封裝類)
2.數(shù)字類型的封裝類,這些類都是Number的抽象子類
Byte,Short,Integer,Long,Float,Double
3.基本類型轉(zhuǎn)封裝類
int i=5;
Integer it = new Integer(i);
Integer it2 = i;//自動裝箱,不用構(gòu)造方法
4.封裝類型轉(zhuǎn)基本類型
Integer it = new Integer(5);
int i = it.intValue();
int i2=it;//自動拆箱
5.int的最大最小值為其對應(yīng)的封裝類Integer.MAX_VALUE等
二.字符串轉(zhuǎn)換
1.數(shù)字轉(zhuǎn)換為字符串
int i = 5;
String str = String.valueof(i);//方法一
Integer it = i;
String str2 = it.toString();//方法二
2.字符串轉(zhuǎn)數(shù)字
使用Integer的靜態(tài)方法parseInt()
三.數(shù)學(xué)方法
java.lang.Math提供了一些常用的數(shù)學(xué)運(yùn)算方法并且都是以靜態(tài)方法的形式
Math.round(float);//四舍五入
Math.random();//0-1間的隨機(jī)數(shù)取不到1
Math.sqrt();//開方
Math.pow(2狞换,4);//2的4次方
Math.PI,Math.E//圓周率和自然數(shù)
四.格式化輸出
1.String name ="蓋倫";
int kill = 8;
String title="超神";
String sentenceFormat ="%s 在進(jìn)行了連續(xù) %d 次擊殺后熟掂,獲得了 %s 的稱號%n";
//使用printf格式化輸出
System.out.printf(sentenceFormat,name,kill,title);
//使用format格式化輸出
System.out.format(sentenceFormat,name,kill,title);
2.
int year = 2020;
//總長度,左對齊爽航,補(bǔ)0蛛勉,千位分隔符儡司,小數(shù)點(diǎn)位數(shù),本地化表達(dá)
//直接打印數(shù)字
System.out.format("%d%n",year);//2020
//總長度是8,默認(rèn)右對齊
System.out.format("%8d%n",year);//....2020
//總長度是8,左對齊
System.out.format("%-8d%n",year);//2020....
//總長度是8,不夠補(bǔ)0
System.out.format("%08d%n",year);//00002020
//千位分隔符
System.out.format("%,8d%n",year*10000);//20,200,000
//小數(shù)點(diǎn)位數(shù)
System.out.format("%.2f%n",Math.PI);//3.14
五.字符char(封裝類Character)
String a = 'a'; //不能夠直接把一個字符轉(zhuǎn)換成字符串
String a2 = Character.toString('a'); //轉(zhuǎn)換為字符串
六.字符串
1.創(chuàng)建字符串
String garen ="蓋倫"; //字面值,虛擬機(jī)碰到字面值就會創(chuàng)建一個字符串對象
String teemo = new String("提莫"); //創(chuàng)建了兩個字符串對象
char[] cs = new char[]{'崔','斯','特'};
String hero = new String(cs);// 通過字符數(shù)組創(chuàng)建一個字符串對象
String hero3 = garen + teemo;// 通過+加號進(jìn)行字符串拼接也會創(chuàng)建新的字符串對象
2.String被修飾為final所以是不能繼承的
3.字符串一旦創(chuàng)建里面的內(nèi)容不能改變
4.String str1 = "the light";
String str2 = new String(str1);
//==用于判斷是否是同一個字符串對象
System.out.println( str1 == str2);
內(nèi)容相同但是不是同一個對象
5.是同一個對象的特例
String str1 = "the light";
String str3 = "the light";
System.out.println( str1 == str3);
6.StringBuffer//可變長度的字符串
String str1 = "let there ";
StringBuffer sb = new StringBuffer(str1); //根據(jù)str1創(chuàng)建一個StringBuffer對象
sb.append("be light"); //在最后追加
System.out.println(sb);
sb.delete(4, 10);//刪除4-10之間的字符
System.out.println(sb);
sb.insert(4, "there ");//在4這個位置插入 there
System.out.println(sb);
sb.reverse(); //反轉(zhuǎn)
System.out.println(sb);
和String一樣渐尿,StringBuffer也維護(hù)了一個字符數(shù)組醉途。 但是,這個字符數(shù)組砖茸,留有冗余長度隘擎。比如說new StringBuffer("the"),其內(nèi)部的字符數(shù)組的長度渔彰,是19(capacity)(不同JDK數(shù)字不一樣)嵌屎,而不是3(length),這樣調(diào)用插入和追加恍涂,在現(xiàn)成的數(shù)組的基礎(chǔ)上就可以完成了宝惰。如果追加的長度超過了19,就會分配一個新的數(shù)組再沧,長度比原來多一些尼夺,把原來的數(shù)據(jù)復(fù)制到新的數(shù)組中,看上去 數(shù)組長度就變長了