把八大基本數據類型封裝到一個類中,並提供屬性和方法,更方便的操做基本數據類型。java
包裝類的出現並非用於取代基本數據類型,也取代不了。正則表達式
包裝類位於java.lang包中。數組
Number 類安全
Number數值類型是byte、double、float、int、long 和 short類的抽象父類,提供把包裝類轉化成基本數據類型的方法(xxxValue)。app
Interger 是int基本數據類型的包裝類,在Integer內部封裝了一個final int value的屬性。學習
[1] 構造方法優化
public static void main(String[] args) {ui // 【1】Integer 屬性編碼 //System.out.println(Integer.MAX_VALUE);spa //System.out.println(Integer.MIN_VALUE);
// 【2】構造方法 int a = 10; String bStr = "20"; Integer i1 = new Integer(a); // 可能拋出NumberFormatException異常 Integer i2 = new Integer(bStr); System.out.println(i1.toString()); System.out.println(i2.toString()); } |
[2] int<->Integer<->string
public static void main(String[] args) {
// int->Integer Integer i1 = new Integer(10); Integer i2 = Integer.valueOf(20);
// Integer->int int a = i1.intValue();
// String->Integer Integer i3 = new Integer("30"); Integer i4 = Integer.valueOf("40");
//Integer->String System.out.println(i3.toString());
// String->int int b = Integer.parseInt("50");
// int->String String str = Integer.toString(10); //String str2 = Integer.toString(8, 2); //System.out.println(str2); } |
[3] Comparable接口
Comparable 表示具備比較能力,對象可比較大小,此接口強行對實現它的每一個類的對象進行總體排序。這種排序被稱爲類的天然排序。
Comparable 定義了a.compareTo(b),返回值表示
a.compareTo(b) |
返回值 |
排序 |
a < b |
負整數 |
升序 |
a = b |
0 |
相等 |
a > b |
正整數 |
降序 |
// 【2】Integer的比較 Integer i2 = new Integer(20); Integer i3 = new Integer(10);
System.out.println(i2.equals(i3)); System.out.println(i2.compareTo(i3)); |
自動裝箱
把基本數據類型自動轉化成對象的包裝類的過程稱爲自動裝箱(auto-boxing)
Integer i = 10; |
把包裝類自動轉化成對於的基本數據類型的過程稱爲自動拆箱(auto-unboxing)
int a = i; |
public static void main(String[] args) {
// Integer i1 = new Integer(10); // 自動裝箱 Integer i2 = 20; // i2 = new Integer(20); System.out.println(i2.toString());
// 自動拆箱 Integer i3 = new Integer(30); int a = i3; // i3.intValue(); } |
注意:
[1]自動裝箱和自動拆箱是jdk1.5
[2]不要過於頻繁的使用拆裝箱操做
其餘包裝類學習方法和Integer徹底同樣。
String 類表明字符串。Java 程序中的全部字符串字面值(如 "abc" )都做爲此類的對象。
字符串本質上是一個字符數組,它們的值在建立以後不能更改,因此字符串是常量;
能夠把字符串看出是字符數組的包裝類,內部聲明一個private final char value[];
由於 String 對象是不可變的,因此能夠共享。
經過字面量建立的字符串分配在常量區。
[1]構造方法
public static void main(String[] args) { // 在堆區初始化一個空字符串 String str1 = new String();
// 經過一個字節數組構建一個字符串 byte[] bytes = {97,98,99}; // 經過使用平臺的默認字符集解碼指定的 byte 數組 // System.out.println(Charset.defaultCharset()); String str2 = new String(bytes); System.out.println(str2);
byte[] byte2 = {-42,-48}; String str3 = null; try { // 使用指定的字符集對字節序列進行解碼 str3 = new String(byte2,"GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println(str3);
// 需求:對一個utf-8的字節序列進行解碼 byte[] byte3 = {-28,-72,-83,-28,-72,-83}; try { // sssString str4 = new String(byte3, "UTF-8"); String str4 = new String(byte3,0,3, "UTF-8"); System.out.println("str4:"+str4); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
// 經過字符數組構建字符串 char[] c1 = {'a','b','c','中','國'}; // String str5 = new String(c1); String str5 = new String(c1,0,3); System.out.println(str5);
String str6 = new String("abc"); String str7 = "abc"; System.out.println(str6 == str7);
} |
[2] 字符串的比較
public static void main(String[] args) { // 【3】字符串比較 String str1 = "abc"; String str2 = "Abc";
System.out.println(str1.compareTo(str2)); // 忽略大小寫比較 System.out.println(str1.compareToIgnoreCase(str2));
// 需求:請輸入驗證碼 /*String validCode = "a4Df"; System.out.println("請輸入驗證碼:"+validCode); Scanner sc = new Scanner(System.in); String inputStr = sc.next();
if(inputStr.compareToIgnoreCase(validCode) == 0) { System.out.println("驗證碼正確"); }else { System.out.println("驗證碼錯誤"); }*/
System.out.println(str1.contentEquals(str2)); System.out.println(str1.equals(str2)); System.out.println(str1.equalsIgnoreCase(str2)); } |
CharSequence 接口把字符串看出一個可讀序列,提供了charAt(index)獲取指定索引處的字符;length() 字符數組或者字符串的長度。
[3]查找、搜索字符串
public static void main(String[] args) { // 【3】查找、搜索字符串中是否包含其餘子串 String str1 = "hello world";
// 是否包含子串 System.out.println(str1.contains("world"));
System.out.println(str1.startsWith("he")); System.out.println(str1.endsWith("world")); System.out.println(str1.startsWith("ll", 2));
// 需求:判斷一個文件是不是png圖片 String fileName = "logo.png"; if(fileName.endsWith(".png")) { System.out.println(fileName+"是一張圖片"); }
// 查找字符串 String str2 = "hello world hello"; // 從左向右查找字符'o'第一次出現的位置,找到返回索引,沒找到返回-1 System.out.println(str2.indexOf('t')); System.out.println(str2.indexOf('o',5)); // 從左向右查找字符串「ll」第一次出現的位置,找到返回索引,沒找到返回-1 System.out.println(str2.indexOf("ll")); System.out.println(str2.indexOf("ll",5));
// 從右向左查找字符'o'第一次出現的位置,找到返回索引,沒找到返回-1 System.out.println(str2.lastIndexOf('o')); // lastIndex(char,fromIndex) // lastIndex(string); // lastIndex(string,fromIndex); } |
[4]格式化字符串
經過指定佔位符(%開始)格式化字符串
%d |
格式化整形 |
%f |
格式化浮點型 |
%.nf |
格式化浮點型四捨五入保留n爲小數, |
%c |
格式化字符 |
%s |
格式化字符串 |
public static void main(String[] args) { // 【4】格式化字符串 float price = 998.126f; int a = 10; int b = 2; // 10 / 2 = 5
String str1 = String.format("%d / %d = %d", a,b,(a/b)); System.out.println(str1); // 四捨五入保留兩位小數 String str2 =String.format("$%.2f", price); System.out.println(str2); } |
[5] 把字符串按指定編碼集編碼成對於的字節序列
// String str3 = "abc"; String str3 = "中國"; // 使用默認的字符集(GBK) byte[] byte1 = str3.getBytes(); System.out.println(Arrays.toString(byte1));
//String str4 = "abc"; String str4 = "中國"; // 使用utf8編碼 byte[] bytes2 = str4.getBytes("UTF-8"); System.out.println(Arrays.toString(bytes2)); |
[6]替換字符串
public static void main(String[] args) {
String str1 = "hello,world"; String newStr1 = str1.replace('o', '8'); System.out.println(newStr1);
String newStr2 = str1.replace("ll", ""); System.out.println(newStr2);
// 需求:186-1234-2234 String str3 = "186-1234-2234"; System.out.println(str3.replace("-", ""));
// 正則表達式專門用於驗證字符串是否符合特定的格式。 String str4 = "6764"; // 驗證str4是否輸入的是一串數字 boolean r = str4.matches("\\d+"); System.out.println(r);
// 需求:abc123te23daf去掉數字 String str5 = "abc123te23daf"; // String newStr5 = str5.replaceAll("\\d+", ""); String newStr5 = str5.replaceFirst("\\d+", ""); System.out.println(newStr5);
} |
[7]拆分字符串
public static void main(String[] args) {
//【7】 根據指定字符串拆分字符串 String str1 = "abc-123"; String[] arr = str1.split("-"); System.out.println(Arrays.toString(arr));
// 需求:請快速構建一個26個小寫英文字母的數組 String str2= "abcdefghijklmnopqrstuvwxyz"; String[] arr2 = str2.split(""); System.out.println(Arrays.toString(arr2));
// 根據正則拆分字符串 String str3 = "abc123ta12asd"; String[] arr3 = str3.split("\\d+"); System.out.println(Arrays.toString(arr3));
// 需求:請獲取一個文件的文件名 String fileName = "logo.png"; String[] arr4 = fileName.split("\\."); System.out.println(Arrays.toString(arr4));
} |
[8] 求子串、大小寫轉換
public static void main(String[] args) {
//【8】 求子串 String str1 = "abc123"; // fromIndex:開始位置,endInde 結束的位置 // 含頭不含尾 String sub1 = str1.substring(0, 3); System.out.println(sub1);
String sub2 = str1.substring(3); System.out.println(sub2);
// 【9】大小寫轉換 String str3 = "Abc"; System.out.println(str3.toUpperCase()); System.out.println(str3.toLowerCase());
} |
[9]其餘方法
public static void main(String[] args) {
// 去掉前導空格和後導空格 String str1 = " abc "; String newStr1 = str1.trim(); System.out.println(str1.length());
// 獲取字符串的字符數組 char[] arr = str1.toCharArray(); System.out.println(Arrays.toString(arr));
// 把其餘數據類型轉化爲字符串 String str2 = String.valueOf(10); System.out.println(str2); } |
1.StringBuffer
StringBuffer是字符的可變容器。能夠在程序運行過程當中向容器中添加、刪除、修改字符。
StringBuffer 本質上是一個字符數組的包裝類,並提供了不少方法向這個字符數組中添加、刪除、修改字符。
StringBuffer 是線程安全的
StringBuffer 內部維護了一個字符數組,默認字符數組的長度是16.當開發者向這個字符數組中添加元素時,若是有額外空間,直接添加到數組中,若是沒有額外空間,StringBuffer內部自動拓容,拓容規則:當前容量*2+2,根據拓容的新空間,複製當前的數組內容到新數組中。
public static void main(String[] args) {
StringBuffer sb = new StringBuffer(); sb.append("a"); sb.append("b"); System.out.println(sb.capacity());
sb.append("1234567890ABCD"); sb.append('x'); System.out.println(sb); System.out.println(sb.capacity());
// 將來若是肯定再也不向sb中添加字符, // 優化內部的數組到指定的長度 sb.trimToSize(); System.out.println(sb.capacity()); } |
package cn.sxt03.string02;
public class Test01 { public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer(); // 【1】添加 sb1.append("hello"); sb1.append('d');
// 返回字符串的長度 System.out.println(sb1.length()); // 返回容器的大小 System.out.println(sb1.capacity());
// 【2】刪除 //System.out.println(sb1); //sb1.delete(0, 5); //System.out.println(sb1);
// 【3】insert(index,t) 在指定位置index添加t sb1.insert(0, "123"); System.out.println(sb1);
// 【4】修改 sb1.setCharAt(0, 'A'); System.out.println(sb1);
// 【5】setLength sb1.setLength(0); // 清空容器內容 sb1.append("中國"); System.out.println(sb1); } }
|
StringBuffer 是線程安全的,執行效率低,JDK1.0
StringBuiler 就是爲了緩解執行效率低而產生的,但線程不安全。JDK 1.5