裝箱拆箱
所有的基本類型,都有對應的類類型。比如int對應的類是Integer,這種類就叫做封裝類
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
//把一個基本類型的變量,轉換為Integer對象
Integer it = new Integer(i);
//把一個Integer對象,轉換為一個基本類型的int
int i2 = it.intValue();
}
}
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLycjNvwFclR3cvwVZ0l2cvw1Ztl2Lc52YuomM39Gavw1LcpDc0RHaiojIsJye.png)
不需要調用構造方法,通過=符号自動把 基本類型 轉換為 類類型 就叫裝箱
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
//基本類型轉換成封裝類型
Integer it = new Integer(i);
//自動轉換就叫裝箱
Integer it2 = i;
}
}
不需要調用Integer的intValue方法,通過=就自動轉換成int類型,就叫拆箱
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
Integer it = new Integer(i);
//封裝類型轉換成基本類型
int i2 = it.intValue();
//自動轉換就叫拆箱
int i3 = it;
}
}
int的最大值可以通過其對應的封裝類Integer.MAX_VALUE擷取
字元串轉換
數字轉字元串
- 方法1: 使用String類的靜态方法valueOf
- 方法2: 先把基本類型裝箱為對象,然後調用對象的toString
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
//方法1
String str = String.valueOf(i);
//方法2
Integer it = i;
String str2 = it.toString();
}
}
字元串轉數字
- 調用Integer的靜态方法parseInt
package digit;
public class TestNumber {
public static void main(String[] args) {
String str = "999";
int i= Integer.parseInt(str);
System.out.println(i);
}
}
數學方法
java.lang.Math提供了一些常用的數學運算方法,并且都是以靜态方法的形式存在
- 四舍五入, 随機數,開方,次方,π,自然常數
格式化輸出
%s 表示字元串
%d 表示數字
%n 表示換行
printf和format能夠達到一模一樣的效果
在不同的作業系統,換行符是不一樣的
(1)在DOS和Windows中,每行結尾是 “\r\n”;
(2)Linux系統裡,每行結尾隻有 “\n”;
(3)Mac系統裡,每行結尾是隻有 "\r"。
為了使得同一個java程式的換行符在所有的作業系統中都有一樣的表現,使用%n,就可以做到平台無關的換行
字元
char對應的封裝類是Character
Character常見方法
package character;
public class TestChar {
public static void main(String[] args) {
System.out.println(Character.isLetter('a'));//判斷是否為字母
System.out.println(Character.isDigit('a')); //判斷是否為數字
System.out.println(Character.isWhitespace(' ')); //是否是空白
System.out.println(Character.isUpperCase('a')); //是否是大寫
System.out.println(Character.isLowerCase('a')); //是否是小寫
System.out.println(Character.toUpperCase('a')); //轉換為大寫
System.out.println(Character.toLowerCase('A')); //轉換為小寫
String a = 'a'; //不能夠直接把一個字元轉換成字元串
String a2 = Character.toString('a'); //轉換為字元串
}
}
字元串
在Java中,字元串是一個類,是以我們見到的字元串都是對象
常見建立字元串手段:
- 每當有一個字面值出現的時候,虛拟機就會建立一個字元串
- 調用String的構造方法建立一個字元串對象
- 通過+加号進行字元串拼接也會建立新的字元串對象
package character;
public class TestString {
public static void main(String[] args) {
String garen ="蓋倫"; //字面值,虛拟機碰到字面值就會建立一個字元串對象
String teemo = new String("提莫"); //建立了兩個字元串對象
char[] cs = new char[]{'崔','斯','特'};
String hero = new String(cs);// 通過字元數組建立一個字元串對象
String hero3 = garen + teemo;// 通過+加号進行字元串拼接
}
}
String 被修飾為final,是以是不能被繼承的
immutable 是指不可改變的
length方法傳回目前字元串的長度
操縱字元串
- charAt(int index)擷取指定位置的字元
- toCharArray()擷取對應的字元數組
- subString截取子字元串
- split根據分隔符進行分隔
- trim去掉首尾空格
- toLowerCase 全部變成小寫
- toUpperCase 全部變成大寫
- indexOf 判斷字元或者子字元串出現的位置
- contains 是否包含子字元串
- replaceAll 替換所有的
- replaceFirst 隻替換第一個
比較字元串
str1和str2的内容一定是一樣的! 但是,并不是同一個字元串對象
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "the light";
String str2 = new String(str1);
//==用于判斷是否是同一個字元串對象
System.out.println( str1 == str2);
}
}
一般說來,編譯器每碰到一個字元串的字面值,就會建立一個新的對象
是以在第6行會建立了一個新的字元串"the light"
但是在第7行,編譯器發現已經存在現成的"the light",那麼就直接拿來使用,而沒有進行重複建立
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "the light";
String str3 = "the light";
System.out.println( str1 == str3);
}
}
- 使用equals進行字元串内容的比較,必須大小寫一緻
- equalsIgnoreCase,忽略大小寫判斷内容是否一緻
是否以子字元串開始或者結束
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "the light";
String start = "the";
String end = "Ight";
System.out.println(str1.startsWith(start));//以...開始
System.out.println(str1.endsWith(end));//以...結束
}
}
StringBuffer
StringBuffer是可變長的字元串
- append追加
- delete 删除
- insert 插入
reverse 反轉
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "let there ";
StringBuffer sb = new StringBuffer(str1); //根據str1建立一個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(); //反轉
System.out.println(sb);
}
}
為什麼StringBuffer可以變長?
- 和String内部是一個字元數組一樣,StringBuffer也維護了一個字元數組。 但是,這個字元數組,留有備援長度