JAVA中String介紹及常見面試題小結
字符串廣泛應用 在 Java 編程中,在 Java 中字符串屬于對象,Java 提供了 String 類來創建和操作字符串。
深刻認識String
1)String為字符串常量:即String對象一旦創建之后該對象是不可更改的。(源碼如下)
String str1 = 'abc'; String str2 = 'abc'; String str3 = new String('abc'); System.out.println(str1 == str2); System.out.println(str1 == str3);
運行結果
truefalse
==比較基本數據類型的時候比較的是值,比較引用類型的時候比較的是地址值。這里是比較的引用類型。 創建“abc”的時候先到常量池看看有沒有,沒有的話就創建一個,有的話就直接用。所以str1和str2的存的地址值相同,指向的是同一個。 因為str3為new出來的對象,new出來的在堆中,而str1在常量池中,所以地址值不可能相等,所以是false。2)How many Objects created with: String str=new String(“abc”)?
String str=new String('abc');
答:創建了兩個對象 一個在堆 一個在常量池
執行“abc”的之后在常量池創建一個,new的時候在堆里創建一個,并把常量池中的“abc”復制了一份過去。然后將其引用賦給了s1。
3) 補充案例
①
String s1 = 'a' + 'b' + 'c';// 在編譯時就變成 abc 常量池中創建abc String s2 = 'abc'; System.out.println(s1 == s2);// true java中有常量優化機制 System.out.println(s1.equals(s2));// true
在編譯時就變成 abc 常量池中創建abc ,兩個都在常量池中
②
String s1 = 'ab'; String s2 = 'abc'; String s3 = s1 + 'c'; System.out.println(s3 == s2);// false System.out.println(s3.equals(s2));// true
因為這里s3相當于new出來的,對應地址在堆中,s2在對應地址在常量池中
Spend a little more time trying to make something of yourself and a little less time trying to impress people.2020.02.25
知識點補充:
String 常見的十種方法
public class ZiFuChuan {public static void main(String[] args) {ZiFuChuanFangFa f=new ZiFuChuanFangFa();f.IndexOf(); //1.字符串查找 注意空格在數組中的位置!字符串查找 indexOf(); 找不到就返回 -1 有就返回此元素在該數組中的角標位置f.chartAt(); //2.獲取指定位置的字符f.substring01(); //3.獲取子字符串!f.substring02(); //在字符串中 截取一部分 有頭無尾!f.startWith(); //4.判斷字符串的開頭和結尾!f.endsWith();f.getBytes(); //5.將字符串轉化為字節 數組!、getBytes();f.toCharArray();//6.將字符串 轉字符 數組!f.length(); //7 返回字符串的長度f.contains(); //8.判斷一個字符串中是否有另一個字符串?f.replace(); //9..字符串替換 可以實現將指定的字符串 和字符 換成新的玩意!oldChar: 要替換的字符或字符串 newChar: 用于替換原來的字符串內容!f.equals(); //10.判斷兩個字符串對象 是否相等! }}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’):查找字符串數組中是否有 p 元素 沒有就返回 '+a);System.out.println('indexOf(‘o’):查找字符串數組中是否有 o 元素 有就返回此元素在該數組中的角標位置 '+a1);}public void equals() { //10.在Java中 判斷兩個對象是否相等 不能“==”表示 會報錯 需要 調用equal()方法!--->比較的字符串的內容是否相等String str= new String('123'); // 演示 對錯!String str01= new String('123');boolean a=str.equals(str01);System.out.println(str ==str01);System.out.println(a);}//2.獲取指定索引位置的字符 在計算機 會直接將 字符轉化成字節 a--->97public void chartAt(){System.out.println();String str='zhaou xu feng';int a=str.charAt(2);System.out.println('chartAt(2):獲取字符串中角標為2的元素! '+a);}//3.獲取子字符串 開頭到所有!public void substring01(){System.out.println();String str='zhou xu feng';String a=str.substring(8); System.out.println('sunstring(2): 獲取字符串中角標為2的元素 ,并取2以后的所有元素 生成一個新的字符串(子字符串!包含的關系) '+a);}//3.1 獲取字符串 區間性 有頭無尾! int beginIndex,int endImdexpublic 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.字符串替換 可以實現將指定的字符串 和字符 換成新的玩意!oldChar: 要替換的字符或字符串 newChar: 用于替換原來的字符串內容!public void replace(){System.out.println();String str='zhou xu feng'; //oldChar: 要替換的字符或字符串String a=str.replace('feng', 'hui'); //newChar: 用于替換原來的字符串內容!System.out.println('replace(qq,cc):字符串替換 后面的替換前面的 '+a);}//5.判斷字符串的開始和結尾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( ):判斷字符串的結束 '+a);}/*6.字母大小寫轉換* str.toLowerCase();* str.toUpperCase();* 7.字符串分割str.split(String sign);*///8.將字符串轉化為字節數組!、getBytes();public void getBytes(){System.out.println();String str='zhaou xu feng';byte[] arr=str.getBytes();System.out.println('getBytes():將字符串轉為字節數組!');for (int i = 0; i < arr.length; i++) {System.out.print(arr[i]+' ');}}//9.將字符串 轉字符數組!public void toCharArray(){System.out.println();System.out.println();String str='zhaou xu feng';System.out.println('getCharArray():將字符串轉為字符組!');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.判斷一個字符串中是否有另一個字符串?public void contains(){String str='zhaou xu feng';boolean a=str.contains('xu');System.out.println('contains(xu):判斷一個字符串中是否有另一個字符串 '+a);}}
總結
到此這篇關于JAVA中String介紹及常見面試題小結的文章就介紹到這了,更多相關java string 面試題內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: