Integer

Interger 是int基本資料型別的包裝類,在Integer內部封裝了一個final int value的屬性。

構造方法:

  Integer類提供了兩種構造方法:它們都會返回一個Integer物件

  (1)Integer(int value);

  (2)Integer(String s);    //要注意的是字串不能包含非數字字元,否則會丟擲NumberFormatException。

  (3)除此之外,還可以給Integer物件直接賦值,如:Integer a = 10;

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);

Comparable介面:

Comparable 表示具有比較能力,物件可比較大小,此介面強行對實現它的每個類的物件進行整體排序。這種排序被稱為類的自然排序。

Comparable 定義了a.compareTo(b),返回值表示:

a.compareTo(b)          返回值               排序

  a<b                      負整數    升序

  a=b          0       相等

  a>b       正整數    降序

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)。

自動拆箱:

把包裝類自動轉化成對應的基本資料型別的過程稱為自動拆箱(auto-unboxing)。

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();

}

String

字串本質:

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);

}

什麼是計算機編碼?

計算機編碼指電腦內部代表字母或數字的方式.常見的編碼方式有:ASCII編碼,GB2312編碼(簡體中文),GBK,BIG5編碼(繁體中文),ANSI編碼,unicode,utf-8編碼等。

為什麼會出現計算機編碼?

計算機編碼能夠將各類符號通過編碼的方式翻譯成二進位制從而讓計算機能夠識別並顯示出來。

[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));

}

[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);

}

 StringBuffer

當對字串進行修改的時候,需要使用 StringBuffer 和 StringBuilder 類。

和 String 類不同的是,StringBuffer 和 StringBuilder 類的物件能夠被多次的修改,並且不產生新的未使用物件。

StringBuilder 類在 Java 5 中被提出,它和 StringBuffer 之間的最大不同在於 StringBuilder 的方法不是執行緒安全的(不能同步訪問)。

由於 StringBuilder 相較於 StringBuffer 有速度優勢,所以多數情況下建議使用 StringBuilder 類。然而在應用程式要求執行緒安全的情況下,則必須使用 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);

}

}

 

StringBuilder

StringBuffer 是執行緒安全的,執行效率低,JDK1.0

StringBuiler 就是為了緩解執行效率低而產生的,但執行緒不安全。JDK 1.5