java系列11:lang包:包裝類、String

一、lang包

java.lang包是java語言(language)內(nèi)置的一個(gè)最基礎(chǔ)的包,其中包含了一系列程序中經(jīng)常要用到的類。
在默認(rèn)情況下,每個(gè)java程序都會(huì)自動(dòng)導(dǎo)入該包,因此無需在程序中顯式地聲明;它是唯一一個(gè)無需顯示聲明就默認(rèn)導(dǎo)入的包。

使用原始數(shù)據(jù)類型聲明的變量,如:

int num = 10;
這里的num只是一個(gè)變量,而不是對象。
在某些必須操作對象的場合,這樣的變量就不能使用了。

lang包的工具有:

  • 包裝類工具
  • 字符串工具
  • 字符串增強(qiáng)工具
  • 數(shù)學(xué)工具(隨機(jī)數(shù)產(chǎn)生工具)
  • Object工具類
    toString
    finalize方法
  • Class工具

二、包裝類

Java提供一系列包裝類,以便將原始數(shù)據(jù)類型當(dāng)作對象進(jìn)行操作。

  • 可以完成基本類型和引用類型的相互轉(zhuǎn)換。
  • 還可以提供特殊的工具方法,來完成某些特定的功能。

在java.lang包中,對于每個(gè)原始數(shù)據(jù)類型都有一個(gè)相應(yīng)的包裝類。

原始數(shù)據(jù)類型和對應(yīng)的包裝類

例:

package test;

public class TestPackageClass {
    public static void main(String[] args) {
        // int -> Integer
        int a = 5;
        Integer b = new Integer(a);
        // Integer -> int
        int c = b.intValue();
    
        String d = "25";
        // String -> Integer
        Integer e1 = new Integer(d);
        Integer e2 = Integer.valueOf(d);
        // String -> int
        int f1 = Integer.parseInt(d);
        int f2 = Integer.valueOf(d).intValue();
    
        // int -> String
        String g1 = a + "";
        String g2 = Integer.toString(a);
        String g3 = String.valueOf(a);
    }
}

三、String

String s = "hello world";

String s = new String("hello world");

Char[] ch = {'h', 'e', 'l', 'l', 'o'};
String s = new String(ch);

byte[] buf = {97, 98, 99, 100, 101};
String s = new String(buf);

1、長度

package test;

public class TestString {
    public static void main(String[] args) {
        String a = "abcbdge";
    
        // 長度:
        System.out.println("a字符串的長度:" + a.length());
    }
}

2、比較

(1)equals和==
equals和==

引用放在棧區(qū)中,內(nèi)容放在堆區(qū)中。

例:

package test;

public class TestString {
    public static void main(String[] args) {
        String str1 = "hello";
        String str2 = "hello";
        String str3 = new String("world");
        String str4 = new String("world");
    
        if(str1 == str2) {
            System.out.println("str1和str2指向同一個(gè)字符串");
        } else {
            System.out.println("str1和str2指向不同的字符串");
        }
    
        if(str1.equals(str2)) {
            System.out.println("str1和str2的內(nèi)容完全相同");
        } else {
            System.out.println("str1和str2的內(nèi)容不相同");
        }
    
        if(str3 == str4) {
            System.out.println("str3和str4指向同一個(gè)字符串");
        } else {
            System.out.println("str3和str4指向不同的字符串");
        }
    
        if(str3.equals(str4)) {
            System.out.println("str3和str4的內(nèi)容完全相同");
        } else {
            System.out.println("str3和str4的內(nèi)容不相同");
        }
    }
}
運(yùn)行結(jié)果
(2)比較大小寫不同的字符串

例:

package test;

public class TestString {
    public static void main(String[] args) {
        String str1 = "Java";
        String str2 = "java";
    
        // 1、忽略大小寫比較
        if(str1.equalsIgnoreCase(str2)) {
            System.out.println("你學(xué)的語言相同");
        } else {
            System.out.println("你學(xué)的語言不同");
        }
    
        // 2、都轉(zhuǎn)換為大寫
        if(str1.toUpperCase().equals(str2.toUpperCase())) {
            System.out.println("你學(xué)的語言相同");
        } else {
            System.out.println("你學(xué)的語言不同");
        }
    
        // 3、都轉(zhuǎn)換為小寫
        if(str1.toLowerCase().equals(str2.toLowerCase())) {
            System.out.println("你學(xué)的語言相同");
        } else {
            System.out.println("你學(xué)的語言不同");
        }
    }
}
(3)按字典順序比較兩個(gè)字符串
public int compareTo(String anotherString)

public int compareToIgnoreCase(String str)

3、連接/拼接

package test;

public class TestString {
    public static void main(String[] args) {
        String str1 = "I";
        String str2 = " love";
        String str3 = " you";
    
        // 方法1:
        System.out.println(str1.concat(str2).concat(str3));
    
        // 方法2:
        System.out.println(str1+str2+str3);
    }
}
4、搜索

搜素某個(gè)字符/某個(gè)子串在字符串中出現(xiàn)的位置。

例:

package test;

public class TestString {
    public static void main(String[] args) {
        String a = "ablovecblovedge";
    
        System.out.println(a.indexOf('b'));
        System.out.println(a.lastIndexOf('b'));
        System.out.println(a.indexOf("love"));
        System.out.println(a.lastIndexOf("love"));
    }
}
運(yùn)行結(jié)果
5、提取
提取

例:

package test;

public class TestString {
    public static void main(String[] args) {
        String str = "  I love you  ";
    
        System.out.println(str.charAt(2));
        System.out.println(str.substring(2));
        System.out.println(str.substring(2, 7));
        System.out.println(str.replace('o', 's'));
        System.out.println(str.trim());
    }
}
運(yùn)行結(jié)果
6、數(shù)據(jù)格式轉(zhuǎn)化

在某些特定的場合,我們可能需要將字符串轉(zhuǎn)化成其它格式的數(shù)據(jù)進(jìn)行操作。

例:

package test;

public class TestString {
    public static void main(String[] args) {
        String str = "  I love you  ";
    
        System.out.println(str.getBytes());
        System.out.println(str.toCharArray());
    }
}
運(yùn)行結(jié)果

例:

package test;

public class TestString {
    public static void main(String[] args) {
        String str = "  I love you  ";
    
        System.out.println(str.getBytes());
    
        // 字符串->字符數(shù)組
        char[] ch = str.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            System.out.println(ch[i]);
        }
    
        // 字符數(shù)組->字符串
        char[] love = {'l', 'o', 'v', 'e'};
        String love2= new String(love);
    
        // 字符串?dāng)?shù)組->字符串:只能通過循環(huán)
        String[] str1 = {"I", "love", "you"};
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < str1.length; i++) {
            sb.append(str1[i]);
        }
        String str2 = sb.toString();
    
        // 數(shù)組的長度:length   字符串的長度:length()
        int a = love.length;
        int b = love2.length();
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容