JAVA中String介紹及常見面試題小結(jié)
字符串廣泛應(yīng)用 在 Java 編程中,在 Java 中字符串屬于對象,Java 提供了 String 類來創(chuàng)建和操作字符串。
深刻認(rèn)識String
1)String為字符串常量:即String對象一旦創(chuàng)建之后該對象是不可更改的。(源碼如下)
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ù)類型的時候比較的是值,比較引用類型的時候比較的是地址值。這里是比較的引用類型。
- 創(chuàng)建“abc”的時候先到常量池看看有沒有,沒有的話就創(chuàng)建一個,有的話就直接用。所以str1和str2的存的地址值相同,指向的是同一個。
- 因?yàn)閟tr3為new出來的對象,new出來的在堆中,而str1在常量池中,所以地址值不可能相等,所以是false。
2)How many Objects created with: String str=new String(“abc”)?
String str=new String("abc");
答:創(chuàng)建了兩個對象 一個在堆 一個在常量池
執(zhí)行“abc”的之后在常量池創(chuàng)建一個,new的時候在堆里創(chuàng)建一個,并把常量池中的“abc”復(fù)制了一份過去。然后將其引用賦給了s1。
3) 補(bǔ)充案例
①
String s1 = "a" + "b" + "c";// 在編譯時就變成 abc 常量池中創(chuàng)建abc String s2 = "abc"; System.out.println(s1 == s2);// true java中有常量優(yōu)化機(jī)制 System.out.println(s1.equals(s2));// true
在編譯時就變成 abc 常量池中創(chuàng)建abc ,兩個都在常量池中
②
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出來的,對應(yīng)地址在堆中,s2在對應(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
知識點(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.判斷一個字符串中是否有另一個字符串?
f.replace(); //9..字符串替換 可以實(shí)現(xiàn)將指定的字符串 和字符 換成新的玩意!oldChar: 要替換的字符或字符串 newChar: 用于替換原來的字符串內(nèi)容!
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'):查找字符串?dāng)?shù)組中是否有 p 元素 沒有就返回 "+a);
System.out.println("indexOf(‘o'):查找字符串?dāng)?shù)組中是否有 o 元素 有就返回此元素在該數(shù)組中的角標(biāo)位置 "+a1);
}
public void equals() { //10.在Java中 判斷兩個對象是否相等 不能“==”表示 會報錯 需要 調(diào)用equal()方法!--->比較的字符串的內(nèi)容是否相等
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.獲取指定索引位置的字符 在計算機(jī) 會直接將 字符轉(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以后的所有元素 生成一個新的字符串(子字符串!包含的關(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.判斷一個字符串中是否有另一個字符串?
public void contains(){
String str="zhaou xu feng";
boolean a=str.contains("xu");
System.out.println("contains(xu):判斷一個字符串中是否有另一個字符串 "+a);
}
}
總結(jié)
到此這篇關(guān)于JAVA中String介紹及常見面試題小結(jié)的文章就介紹到這了,更多相關(guān)java string 面試題內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
request如何獲取body的json數(shù)據(jù)
這篇文章主要介紹了request如何獲取body的json數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringBoot中注解實(shí)現(xiàn)定時任務(wù)的兩種方式
這篇文章主要介紹了SpringBoot中注解實(shí)現(xiàn)定時任務(wù)的兩種方式,SpringBoot 定時任務(wù)是一種在SpringBoot應(yīng)用中自動執(zhí)行任務(wù)的機(jī)制,通過使用Spring框架提供的@Scheduled注解,我們可以輕松地創(chuàng)建定時任務(wù),需要的朋友可以參考下2023-10-10
Java FileDescriptor總結(jié)_動力節(jié)點(diǎn)Java學(xué)院整理
FileDescriptor 是“文件描述符”??梢员挥脕肀硎鹃_放文件、開放套接字等。接下來通過本文給大家分享Java FileDescriptor總結(jié),感興趣的朋友一起學(xué)習(xí)吧2017-05-05
java并發(fā)編程專題(一)----線程基礎(chǔ)知識
這篇文章主要介紹了java并發(fā)編程線程的基礎(chǔ)知識,文中講解非常詳細(xì),幫助大家更好的學(xué)習(xí)JAVA并發(fā)編程,感興趣想學(xué)習(xí)JAVA的可以了解下2020-06-06
Java Springboot 后端使用Mockito庫進(jìn)行單元測試流程分析
使用Mock進(jìn)行單元測試可以避免啟動整個Spring框架,節(jié)省時間并降低外部依賴影響,Mock允許模擬外部方法和類,專注于測試方法的功能邏輯,本文給大家介紹Java Springboot 后端使用Mockito庫進(jìn)行單元測試流程分析,感興趣的朋友跟隨小編一起看看吧2024-10-10

