JAVA中String介紹及常見面試題小結(jié)
字符串廣泛應(yīng)用 在 Java 編程中,在 Java 中字符串屬于對(duì)象,Java 提供了 String 類來創(chuàng)建和操作字符串。
深刻認(rèn)識(shí)String
1)String為字符串常量:即String對(duì)象一旦創(chuàng)建之后該對(duì)象是不可更改的。(源碼如下)
String str1 = "abc"; String str2 = "abc"; String str3 = new String("abc"); System.out.println(str1 == str2); System.out.println(str1 == str3);
運(yùn)行結(jié)果
true
false
- ==比較基本數(shù)據(jù)類型的時(shí)候比較的是值,比較引用類型的時(shí)候比較的是地址值。這里是比較的引用類型。
- 創(chuàng)建“abc”的時(shí)候先到常量池看看有沒有,沒有的話就創(chuàng)建一個(gè),有的話就直接用。所以str1和str2的存的地址值相同,指向的是同一個(gè)。
- 因?yàn)閟tr3為new出來的對(duì)象,new出來的在堆中,而str1在常量池中,所以地址值不可能相等,所以是false。
2)How many Objects created with: String str=new String(“abc”)?
String str=new String("abc");
答:創(chuàng)建了兩個(gè)對(duì)象 一個(gè)在堆 一個(gè)在常量池
執(zhí)行“abc”的之后在常量池創(chuàng)建一個(gè),new的時(shí)候在堆里創(chuàng)建一個(gè),并把常量池中的“abc”復(fù)制了一份過去。然后將其引用賦給了s1。
3) 補(bǔ)充案例
①
String s1 = "a" + "b" + "c";// 在編譯時(shí)就變成 abc 常量池中創(chuàng)建abc String s2 = "abc"; System.out.println(s1 == s2);// true java中有常量優(yōu)化機(jī)制 System.out.println(s1.equals(s2));// true
在編譯時(shí)就變成 abc 常量池中創(chuàng)建abc ,兩個(gè)都在常量池中
②
String s1 = "ab"; String s2 = "abc"; String s3 = s1 + "c"; System.out.println(s3 == s2);// false System.out.println(s3.equals(s2));// true
因?yàn)檫@里s3相當(dāng)于new出來的,對(duì)應(yīng)地址在堆中,s2在對(duì)應(yīng)地址在常量池中
Spend a little more time trying to make something of yourself and a little less time trying to impress people.
2020.02.25
知識(shí)點(diǎn)補(bǔ)充:
String 常見的十種方法
public class ZiFuChuan { public static void main(String[] args) { ZiFuChuanFangFa f=new ZiFuChuanFangFa(); f.IndexOf(); //1.字符串查找 注意空格在數(shù)組中的位置!字符串查找 indexOf(); 找不到就返回 -1 有就返回此元素在該數(shù)組中的角標(biāo)位置 f.chartAt(); //2.獲取指定位置的字符 f.substring01(); //3.獲取子字符串! f.substring02(); //在字符串中 截取一部分 有頭無尾! f.startWith(); //4.判斷字符串的開頭和結(jié)尾! f.endsWith(); f.getBytes(); //5.將字符串轉(zhuǎn)化為字節(jié) 數(shù)組!、getBytes(); f.toCharArray();//6.將字符串 轉(zhuǎn)字符 數(shù)組! f.length(); //7 返回字符串的長度 f.contains(); //8.判斷一個(gè)字符串中是否有另一個(gè)字符串? f.replace(); //9..字符串替換 可以實(shí)現(xiàn)將指定的字符串 和字符 換成新的玩意!oldChar: 要替換的字符或字符串 newChar: 用于替換原來的字符串內(nèi)容! f.equals(); //10.判斷兩個(gè)字符串對(duì)象 是否相等! } } class ZiFuChuanFangFa{ private static final char oldChar = 0; public void IndexOf() { String str="Ni Hao "; int a=str.indexOf("p"); int a1=str.indexOf("i"); System.out.println("indexOf(‘p'):查找字符串?dāng)?shù)組中是否有 p 元素 沒有就返回 "+a); System.out.println("indexOf(‘o'):查找字符串?dāng)?shù)組中是否有 o 元素 有就返回此元素在該數(shù)組中的角標(biāo)位置 "+a1); } public void equals() { //10.在Java中 判斷兩個(gè)對(duì)象是否相等 不能“==”表示 會(huì)報(bào)錯(cuò) 需要 調(diào)用equal()方法!--->比較的字符串的內(nèi)容是否相等 String str= new String("123"); // 演示 對(duì)錯(cuò)! String str01= new String("123"); boolean a=str.equals(str01); System.out.println(str ==str01); System.out.println(a); } //2.獲取指定索引位置的字符 在計(jì)算機(jī) 會(huì)直接將 字符轉(zhuǎn)化成字節(jié) a--->97 public void chartAt(){ System.out.println(); String str="zhaou xu feng"; int a=str.charAt(2); System.out.println("chartAt(2):獲取字符串中角標(biāo)為2的元素! "+a); } //3.獲取子字符串 開頭到所有! public void substring01(){ System.out.println(); String str="zhou xu feng"; String a=str.substring(8); System.out.println("sunstring(2): 獲取字符串中角標(biāo)為2的元素 ,并取2以后的所有元素 生成一個(gè)新的字符串(子字符串!包含的關(guān)系) "+a); } //3.1 獲取字符串 區(qū)間性 有頭無尾! int beginIndex,int endImdex public void substring02(/*int beginIndex,int endImdex*/){ System.out.println(); String str="zhou xu feng"; String a=str.substring(1, 4); //可以在 字符串中截取一部分 System.out.println("substring(1,4: 在字符串中截取一部分 有頭無尾!) "+a); } //4.字符串替換 可以實(shí)現(xiàn)將指定的字符串 和字符 換成新的玩意!oldChar: 要替換的字符或字符串 newChar: 用于替換原來的字符串內(nèi)容! public void replace(){ System.out.println(); String str="zhou xu feng"; //oldChar: 要替換的字符或字符串 String a=str.replace("feng", "hui"); //newChar: 用于替換原來的字符串內(nèi)容! System.out.println("replace(qq,cc):字符串替換 后面的替換前面的 "+a); } //5.判斷字符串的開始和結(jié)尾 public void startWith(){ System.out.println(); String str="zhaou xu feng"; boolean a=str.startsWith(str); System.out.println("startsWith(str):判斷字符串的開始 "+a); } public void endsWith( ){ System.out.println(); String str="zhaou xu feng"; boolean a=str.endsWith("g"); System.out.println("endsWith( ):判斷字符串的結(jié)束 "+a); } /*6.字母大小寫轉(zhuǎn)換 * str.toLowerCase(); * str.toUpperCase(); * 7.字符串分割 str.split(String sign); */ //8.將字符串轉(zhuǎn)化為字節(jié)數(shù)組!、getBytes(); public void getBytes(){ System.out.println(); String str="zhaou xu feng"; byte[] arr=str.getBytes(); System.out.println("getBytes():將字符串轉(zhuǎn)為字節(jié)數(shù)組!"); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } } //9.將字符串 轉(zhuǎn)字符數(shù)組! public void toCharArray() { System.out.println(); System.out.println(); String str="zhaou xu feng"; System.out.println("getCharArray():將字符串轉(zhuǎn)為字符組!"); char []arr=str.toCharArray(); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]); } } //10 返回字符串的長度 public void length(){ System.out.println(); String str="zhaou xu feng"; int a= str.length(); System.out.println("length():返回字符串的長度!"+a); } //11.判斷一個(gè)字符串中是否有另一個(gè)字符串? public void contains(){ String str="zhaou xu feng"; boolean a=str.contains("xu"); System.out.println("contains(xu):判斷一個(gè)字符串中是否有另一個(gè)字符串 "+a); } }
總結(jié)
到此這篇關(guān)于JAVA中String介紹及常見面試題小結(jié)的文章就介紹到這了,更多相關(guān)java string 面試題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jmeter自定義函數(shù)base64加密實(shí)現(xiàn)過程解析
這篇文章主要介紹了Jmeter自定義函數(shù)base64加密實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07關(guān)于spring data jpa一級(jí)緩存的問題
這篇文章主要介紹了關(guān)于spring data jpa一級(jí)緩存的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11java 自己實(shí)現(xiàn)DataSource實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了java 自己實(shí)現(xiàn)DataSource實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05mybatis中使用InsertProvider注解報(bào)錯(cuò)解決全過程
這篇文章主要介紹了mybatis中使用InsertProvider注解報(bào)錯(cuò)解決全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Springboot actuator應(yīng)用后臺(tái)監(jiān)控實(shí)現(xiàn)
這篇文章主要介紹了Springboot actuator應(yīng)用后臺(tái)監(jiān)控實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04