阿里最新面试必备项之Java的String类,持续更新中!
最新騰訊面試必備項(xiàng)之Java的String類(lèi),持續(xù)更新中!
1.1 String的特性
-
String類(lèi):代表字符串。Java程序中的所有字符串字面值(如“abc”)都作為此類(lèi)的實(shí)例實(shí)現(xiàn)。
-
String是一個(gè)final類(lèi),代表不可變的字符序列。
-
String字符串是常量,用雙引號(hào)引起來(lái)表示。他們的值在創(chuàng)建之后不能更改。
-
String對(duì)象的找字符內(nèi)容是存儲(chǔ)在一個(gè)字符數(shù)組value[]中的。(jdk新版本已改為使用byte類(lèi)型的數(shù)組value[]存放)
1.2 String字面量賦值的內(nèi)存理解
字面量賦值是直接在常量池中賦值的
Demo:
package com.broky.commonClass;import org.junit.jupiter.api.Test;/*** String 的使用** @author 13roky* @date 2021-04-24 10:34*/ public class StringTest {/*String:字符串,使用一對(duì)""來(lái)表示.1.String類(lèi)是被聲明為final的,不可被繼承.2.String實(shí)現(xiàn)了Serializable接口:標(biāo)識(shí)字符串是支持序列化的. (io流)實(shí)現(xiàn)了Comparable接口:可以比較大小.3.String內(nèi)部定義了final char[] value用于存儲(chǔ)字符串?dāng)?shù)字. final表示數(shù)組和其元素不能被修改。(為了節(jié)省jvm的內(nèi)存空間jdk9已經(jīng)改為byte[]類(lèi)型數(shù)組)4.String:代表不可變的字符序列。簡(jiǎn)稱(chēng):不可變性。體現(xiàn):1.當(dāng)對(duì)字符串重新賦值時(shí),需要重新指定內(nèi)存區(qū)域賦值,不能使用原有的value進(jìn)行賦值.(因?yàn)樵械膙alue是final的)2.當(dāng)對(duì)現(xiàn)有的字符串進(jìn)行連接操作時(shí),需要重新指定內(nèi)存區(qū)域賦值,不能使用原有的value賦值.3.當(dāng)調(diào)用String的replace()方法修改字符或字符串時(shí),也必須重新指定內(nèi)存區(qū)域賦值,不能使用原有的value賦值.5.通過(guò)字面量的方式(區(qū)別于new)給一個(gè)字符串賦值,此時(shí)的字符串值生命在字符串常量池中.6.字符串常量池中是不會(huì)存儲(chǔ)相同內(nèi)容的字符串的.*/@Testpublic void test01(){//字面量的定義方式, 在內(nèi)存中用的是同一個(gè)內(nèi)存地址String s1 = "abc";String s2 = "abc";//==比較的是地址值,為true說(shuō)明s1和s2在內(nèi)存中指向的是同一個(gè)位置System.out.println(s1 == s2);//trues1 = "hello";System.out.println(s1);//helloSystem.out.println(s2);//abcSystem.out.println("================================================");String s3 = "abc";s3 += "def";System.out.println(s3);//abcdefSystem.out.println(s2);//abcSystem.out.println("================================================");String s4 = "adb";String s5 = s4.replace('a','m');System.out.println(s4);//abcSystem.out.println(s5);//mbc} }圖解:
由于字符串常量池中是不會(huì)存儲(chǔ)相同內(nèi)容的字符串的,所以在字符串常量池中s1和s2指向同一個(gè)內(nèi)存地址。
由于String內(nèi)部定義了final char[] value用于存儲(chǔ)字符串?dāng)?shù)字,final表示數(shù)組和其元素不能被修改,其也就有了不可變的字符序列的性質(zhì)。所以改變s1取值為hello后,并不會(huì)改變字符串常量池中的對(duì)應(yīng)位置的值,而是會(huì)新開(kāi)辟一個(gè)內(nèi)存地址存放hello值,并且s1指向新的內(nèi)存地址。
以下圖解類(lèi)似。
1.3 String new方式賦值的內(nèi)存理解
Demo:
package com.broky.commonClass;import org.junit.jupiter.api.Test;/*** String 的使用** @author 13roky* @date 2021-04-24 10:34*/ public class StringTest {/*String實(shí)例化方式測(cè)試:方式一: 通過(guò)字面量定義的方式方式二: 通過(guò)new + 構(gòu)造器的方式面試題:String s = new String("abc);方式創(chuàng)建對(duì)象,在內(nèi)存中創(chuàng)建了幾個(gè)對(duì)象?兩個(gè):一個(gè)是堆空間中new結(jié)構(gòu),另一個(gè)是char[]對(duì)應(yīng)的常量池中的數(shù)據(jù)"abc"*/@Testpublic void test2() {//通過(guò)字面量定義的方式:此時(shí)的s1和s2的數(shù)據(jù)javaEE生命在方法區(qū)中的字符串常量池中.String s1 = "javaEE";String s2 = "javaEE";//通過(guò)new + 構(gòu)造器的方式:此時(shí)的s3和s4保存的地址值是數(shù)據(jù)在堆空間中開(kāi)辟空間后對(duì)應(yīng)的地址值.String s3 = new String("javaEE");String s4 = new String("javaEE");System.out.println(s1 == s2);//trueSystem.out.println(s1 == s3);//falseSystem.out.println(s1 == s4);//falseSystem.out.println(s3 == s4);//falseSystem.out.println(s3.equals(s4));//trueSystem.out.println("=================================================");Person p1 = new Person("Tom",12);Person p2 = new Person("Tom",12);System.out.println(p1.name.equals(p2.name));//trueSystem.out.println(p1.name == p2.name);//true} }//加入Java開(kāi)發(fā)交流君樣:756584822一起吹水聊天class Person{public String name;public int age;public Person(String name,int age) {this.name = name;this.age = age;} }圖解:
new的結(jié)構(gòu)是存在于堆中的,比如String s3 = new String("javaEE");
1.4 String 拼接字面量和變量的方式賦值
Demo:
package com.broky.commonClass;import org.junit.jupiter.api.Test;/*** String 的使用** @author 13roky* @date 2021-04-24 10:34*/ public class StringTest {/*1.常量與常量的拼接結(jié)果在常量池。且常量池中不會(huì)存在享同內(nèi)容的常量。2.只要其中有一個(gè)是變量,結(jié)果就在堆中。3.如果拼接的結(jié)果調(diào)用intern()方法,返回值就會(huì)在常量池中。*/@Testpublic void test03() {String s1 = "javaEE";String s2 = "hadoop";String s3 = "javaEEhadoop";String s4 = "javaEE" + "hadoop";//引號(hào)中的為字面量,這里是字面量的拼接String s5 = s1 + "hadoop";String s6 = "javaEE" + s2;String s7 = s1 + s2;final String s8 = "hadoop";String s9 = "javaEE" + s8;System.out.println(s3 == s4);//trueSystem.out.println(s3 == s5);//falseSystem.out.println(s3 == s6);//falseSystem.out.println(s3 == s7);//falseSystem.out.println(s5 == s6);//falseSystem.out.println(s5 == s7);//falseSystem.out.println(s6 == s7);//falseSystem.out.println(s3 == s9);//trueString s10 = s5.intern();//返回值得到的s8使用的常量值中已經(jīng)存在的”javaEEhadoop“(s5.intern返回的時(shí)常量池中對(duì)應(yīng)的內(nèi)存地址)System.out.println(s3 == s10);//true} }圖解:
- 常量與常量的拼接,結(jié)果直接保存在常量池中。如String s4 = “javaEE” + “hadoop”;,如果常量池中存在“javaEEhadoop”,那么s4直接指向其地址。
- 只要拼接賦值時(shí),其中有一個(gè)是變量,那么結(jié)果就會(huì)存在于堆中,如String s5 = s1 + “hadoop”;,棧中的變量名s5指向堆中對(duì)應(yīng)的地址0x0001,堆中的地址又指向常量池的地址0x1214。
- s5指向的是堆中的內(nèi)存地址0x0001,但是方法s5.intern返回的直接是常量池中的地址。所以String s10 = s5.intern();這行代碼會(huì)讓s10直接指向常量池對(duì)應(yīng)的內(nèi)存地址。
1.5 String類(lèi)常用方法
- int Length():返回字符的長(zhǎng)度:return value.Length
- char charAt(int index):返回某索引處的字return vaLue[index]
- booLean isEmpty():判斷是否是空字符牢:return value. Length == 0`
- String toLowercase():使用默認(rèn)語(yǔ)言環(huán)境,將 String中的所有字符轉(zhuǎn)換為小寫(xiě)
- String toUppercase():使用默認(rèn)語(yǔ)言環(huán)境,將 String中的所有字符轉(zhuǎn)換為大寫(xiě)
- String trim():返回字符的副本,忽略前導(dǎo)空白和尾部空白
- boolean equals(Object obj):比較字符的內(nèi)容是否相同
- booLean equalsIgnoreCase(String anotherString):與equls方法類(lèi)似,忽略大小寫(xiě)
- String concat(string str):將指定字符牢連接到此字符的結(jié)尾。等價(jià)于用"+"
- int compare To(String anotherstring):比較兩個(gè)字符的大小
- String substring(int beginIndex):返回一個(gè)新的字符,它是此字符的從 beginIndex開(kāi)始截取到最后一個(gè)子字符串.
- String substring(int beginIndex, int endindex):返回一個(gè)新字符串,它是此字符從beginIndex開(kāi)始截取到endIndex(不包含)的一個(gè)子字符串.
Demo:
package com.broky.commonClass;import org.junit.jupiter.api.Test;import java.util.Locale;/*** @author 13roky* @date 2021-04-26 21:47*/ public class CommonMethod {/*int Length():返回字符的長(zhǎng)度: return value.Lengthchar charAt( nt index):返回某索引處的字return vaLue[index]booLean isEmpty():判斷是否是空字符牢: return value. Length == 0String toLowercase():使用默認(rèn)語(yǔ)言環(huán)境,將 String中的所有字符轉(zhuǎn)換為小寫(xiě)String toUppercase():使用默認(rèn)語(yǔ)言環(huán)境,將 String中的所有字符轉(zhuǎn)換為大寫(xiě)String trim():返園字符的副本,忽略前導(dǎo)空白和尾部空白boolean equals(Object obj):比較字符的內(nèi)容是否相同booLean equalsIgnoreCase(String anotherString):與equls方法類(lèi)似,忽略大小寫(xiě)String concat(string str):將指定字符牢連接到此字符的結(jié)尾。等價(jià)于用"+"int compare To(String anotherstring):比較兩個(gè)字符的大小String substring(int beginIndex):返回一個(gè)新的字符,它是此字符的從 beginIndex開(kāi)始截取到最后一個(gè)子字符串.String substring(int beginIndex, int endindex):返回一個(gè)新字符串,它是此字符從beginIndex開(kāi)始截取到endIndex(不包含)的一個(gè)子字符串.*/@Testpublic void test01(){String s1 ="HelloWorld";System.out.println(s1.length());System.out.println(s1.charAt(0));System.out.println(s1.charAt(9));System.out.println(s1.isEmpty());String s2 = s1.toLowerCase();System.out.println(s1);System.out.println(s2);String s3 = " he llo world ";String s4 = s3.trim();System.out.println(s3);System.out.println(s4);}@Testpublic void test02(){String s1 = "HelloWorld";String s2 = "helloworld";System.out.println(s1.equals(s2));System.out.println(s1.equalsIgnoreCase(s2));String s3 = "abc";String s4 = "def".concat(s3);System.out.println(s4);String s5 = "abc";String s6 = new String("abd");System.out.println(s5.compareTo(s6));String s7 = "13roky學(xué)Java";String s8 = s7.substring(2,6);System.out.println(s7);System.out.println(s8);} }- boolean endsWith(String suffix):測(cè)試此字符串是否以指定的后綴結(jié)束
- boolean startsWith(String prefix):測(cè)試此字符串是否以指定的前綴開(kāi)始
- boolean startsWith(String prefix, int toffset):測(cè)試此字符串從指定索引開(kāi)始的子字符串是否以指定的前綴開(kāi)始
- boolean contains(CharSequence s):當(dāng)且僅當(dāng)此字符串包含指定的char值序列時(shí),返回true
- int indexOf(String str): 返回指定子字符串在此字符串中第一次出現(xiàn)處的索引
- int indexOf(String str,int fromIndex):返回指定子字符串在此字符串中第一次出現(xiàn)處的索引,從指定的索引處開(kāi)始
- int lastIndexOf(String str):返回指定子字符串在此字符串中最右邊出現(xiàn)處的索引
- int lastIndexOf(String str,int fromIndex):返回指定子字符串在此字符串中最后一次出現(xiàn)處的索引,從指定的索引開(kāi)始反向搜索(從右往左搜索)indexOf和lastindexOf方法如果未找到,返回結(jié)果都是-1
Demo:
package com.broky.commonClass;import jdk.jfr.DataAmount; import org.junit.jupiter.api.Test;import java.util.Locale;/*** @author 13roky* @date 2021-04-26 21:47*/ public class CommonMethod {/*boolean endsWith(String suffix):測(cè)試此字符串是否以指定的后綴結(jié)束boolean startsWith(String prefix):測(cè)試此字符串是否以指定的前綴開(kāi)始boolean startsWith(String prefix, int toffset):測(cè)試此字符串從指定索引開(kāi)始的子字符串是否以指定的前綴開(kāi)始boolean contains(CharSequence s):當(dāng)且僅當(dāng)此字符串包含指定的char值序列時(shí),返回trueint indexOf(String str): 返回指定子字符串在此字符串中第一次出現(xiàn)處的索引int indexOf(String str,int fromIndex):返回指定子字符串在此字符串中第一次出現(xiàn)處的索引,從指定的索引處開(kāi)始int lastIndexOf(String str):返回指定子字符串在此字符串中最右邊出現(xiàn)處的索引int lastIndexOf(String str,int fromIndex):返回指定子字符串在此字符串中最后一次出現(xiàn)處的索引,從指定的索引開(kāi)始反向搜索(從右往左搜索)indexOf和lastindexOf方法如果未找到,返回結(jié)果都是-1*/@Testpublic void test03(){String str1 = "helloworld";boolean b1 = str1.endsWith("rld");System.out.println(b1);boolean b2 = str1.startsWith("He");System.out.println(b2);boolean b3 =str1.startsWith("ll",2);System.out.println(b3);String str2 = "wo";System.out.println(str1.contains(str2));System.out.println(str1.indexOf("lol"));System.out.println(str1.indexOf("l"));System.out.println(str1.indexOf("lo", 5));String str3 = "hellorworld";System.out.println(str3.lastIndexOf("or"));System.out.println(str3.lastIndexOf("or",6));}//加入Java開(kāi)發(fā)交流君樣:756584822一起吹水聊天//什么情況下,indexOf(str)和lastIndexOf(str)返回值相同?//情況一:存在唯一的一個(gè)str.//情況二:不存在str }替換:
String replace(char oldChar,char newChar):返回一個(gè)新的字符串,它是通過(guò)用newChar替換oldChar String replace(CharSequence target,CharSequence replacement):使用字面值替換序列替換此字符串所有匹配字面值目標(biāo)序列的子字符串.
String replaceAll(String regex,String replacement):使用給定的replacement替換此字符串多有匹配給定的正則表達(dá)式的子字符串
String replaceFirst(String regex,String replacement):使用給定的replacement替換此字符串匹配給定的正則表達(dá)式的第一個(gè)子字符串.
匹配:
boolean matches(String regex):告知此字符串是否匹配給定得正則表達(dá)式
切片:
String[] split(String regex):根據(jù)給定的正則表達(dá)式的匹配拆分此字符串
String[] split(String regex,int limit):根據(jù)匹配給定的正則表達(dá)式來(lái)分此字符串,最多不超過(guò)limit個(gè),如果超出,剩下的全部都放到最后一個(gè)元素
1.6 String與其它類(lèi)型的轉(zhuǎn)換
demo:
package com.broky.commonClass;import org.junit.jupiter.api.Test;import java.io.UnsupportedEncodingException; import java.util.Arrays;/*** String類(lèi)與其他結(jié)構(gòu)之間的轉(zhuǎn)換* String 與 char[] 之間的轉(zhuǎn)換* String 與 byte[] 之間的轉(zhuǎn)換** @author 13roky* @date 2021-05-02 19:33*/ public class StringChange {/*復(fù)習(xí):String與其他數(shù)據(jù)類(lèi)型,包裝類(lèi)之間的轉(zhuǎn)換String --> 基本數(shù)據(jù)類(lèi)型、包裝類(lèi):調(diào)用包裝類(lèi)的靜態(tài)方法:parseXxx(str)基本數(shù)據(jù)類(lèi)型、包裝類(lèi) ——》String:調(diào)用String重載的valueOf(xxx) 或者直接拼接“”*/@Testpublic void test(){String str = "123";//int num = (int) str; 只有子父類(lèi)的關(guān)系才可以使用強(qiáng)制類(lèi)型轉(zhuǎn)換int num = Integer.parseInt(str);String str2 = String.valueOf(num);String str3 = num + "";}/*//加入Java開(kāi)發(fā)交流君樣:756584822一起吹水聊天String 與 char[] 之間的轉(zhuǎn)換String --> char[] :String類(lèi)中的toCharArray()方法char[] --> String :String的構(gòu)造器*/@Testpublic void test02(){String str = "abcde";char[] c1 = str.toCharArray();for (int i = 0; i < c1.length; i++) {System.out.println(c1[i]);}char[] c2 = new char[]{'f','s','c','a'};String str2 = new String(c2);System.out.println(str2);}/*String 與 byte[] 之間的轉(zhuǎn)換編碼:String --> byte[] :調(diào)用String的getBytes()解碼:轉(zhuǎn)化的時(shí)候會(huì)涉及編碼和解碼編碼:字符串 --> 字節(jié) (看得懂轉(zhuǎn)換為看不懂的二進(jìn)制數(shù)據(jù))解碼 字節(jié) --> 字符串 (看不懂的二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為看得懂)*/@Testpublic void test03() throws UnsupportedEncodingException {String str = "abc123此方";// 使用ide默認(rèn)的編碼集進(jìn)行轉(zhuǎn)換byte[] b1 = str.getBytes();// 字節(jié)byte類(lèi)型 采用ASCLL編碼 由于ASCLL中沒(méi)有中文編碼,所以中文會(huì)轉(zhuǎn)為默認(rèn)的編碼如(UTF-8,UTF-8中一個(gè)漢字占三位)然后再轉(zhuǎn)為ASCLLSystem.out.println(Arrays.toString(b1));// 使用 gbk 字符集進(jìn)行編碼,需要處理異常byte[] b2 = str.getBytes("gbk");System.out.println(Arrays.toString(b2));System.out.println("=======================================");// 使用ide默認(rèn)的編碼集進(jìn)行解碼String str2 = new String(b1);System.out.println(str2);// 出現(xiàn)亂碼。原因:編碼及和解碼集不一致倒置的String str3 = new String(b2);System.out.println(str3);// 指定編碼集String str4 = new String(b2, "gbk");System.out.println(str4);} }1.7 常見(jiàn)算法題目
最后,祝大家早日學(xué)有所成,拿到滿意offer
總結(jié)
以上是生活随笔為你收集整理的阿里最新面试必备项之Java的String类,持续更新中!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 不能再叫“大学”了:京东方大学宣布改名为
- 下一篇: 世道变了,面试初级Java开发会问到Ar