久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

JAVA中String介紹及常見(jiàn)面試題小結(jié)

瀏覽:90日期:2022-09-05 10:58:03

字符串廣泛應(yīng)用 在 Java 編程中,在 Java 中字符串屬于對(duì)象,Java 提供了 String 類來(lái)創(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é)果

truefalse

==比較基本數(shù)據(jù)類型的時(shí)候比較的是值,比較引用類型的時(shí)候比較的是地址值。這里是比較的引用類型。 創(chuàng)建“abc”的時(shí)候先到常量池看看有沒(méi)有,沒(méi)有的話就創(chuàng)建一個(gè),有的話就直接用。所以str1和str2的存的地址值相同,指向的是同一個(gè)。 因?yàn)閟tr3為new出來(lái)的對(duì)象,new出來(lái)的在堆中,而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ù)制了一份過(guò)去。然后將其引用賦給了s1。

3) 補(bǔ)充案例

String s1 = 'a' + 'b' + 'c';// 在編譯時(shí)就變成 abc 常量池中創(chuàng)建abc String s2 = 'abc'; System.out.println(s1 == s2);// true java中有常量?jī)?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出來(lái)的,對(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 常見(jiàn)的十種方法

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(); //在字符串中 截取一部分 有頭無(wú)尾!f.startWith(); //4.判斷字符串的開(kāi)頭和結(jié)尾!f.endsWith();f.getBytes(); //5.將字符串轉(zhuǎn)化為字節(jié) 數(shù)組!、getBytes();f.toCharArray();//6.將字符串 轉(zhuǎn)字符 數(shù)組!f.length(); //7 返回字符串的長(zhǎng)度f(wàn).contains(); //8.判斷一個(gè)字符串中是否有另一個(gè)字符串?f.replace(); //9..字符串替換 可以實(shí)現(xiàn)將指定的字符串 和字符 換成新的玩意!oldChar: 要替換的字符或字符串 newChar: 用于替換原來(lái)的字符串內(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 元素 沒(méi)有就返回 '+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--->97public 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.獲取子字符串 開(kāi)頭到所有!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ū)間性 有頭無(wú)尾! 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: 在字符串中截取一部分 有頭無(wú)尾!) '+a);}//4.字符串替換 可以實(shí)現(xiàn)將指定的字符串 和字符 換成新的玩意!oldChar: 要替換的字符或字符串 newChar: 用于替換原來(lái)的字符串內(nèi)容!public void replace(){System.out.println();String str='zhou xu feng'; //oldChar: 要替換的字符或字符串String a=str.replace('feng', 'hui'); //newChar: 用于替換原來(lái)的字符串內(nèi)容!System.out.println('replace(qq,cc):字符串替換 后面的替換前面的 '+a);}//5.判斷字符串的開(kāi)始和結(jié)尾public void startWith(){System.out.println(); String str='zhaou xu feng';boolean a=str.startsWith(str);System.out.println('startsWith(str):判斷字符串的開(kāi)始 '+a);}public void endsWith( ){System.out.println();String str='zhaou xu feng';boolean a=str.endsWith('g');System.out.println('endsWith( ):判斷字符串的結(jié)束 '+a);}/*6.字母大小寫(xiě)轉(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 返回字符串的長(zhǎng)度public void length(){System.out.println();String str='zhaou xu feng';int a= str.length();System.out.println('length():返回字符串的長(zhǎng)度!'+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àn)面試題小結(jié)的文章就介紹到這了,更多相關(guān)java string 面試題內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 丹东市| 荔浦县| 卫辉市| 茌平县| 佳木斯市| 寿光市| 阳西县| 黄山市| 神农架林区| 黄大仙区| 江油市| 侯马市| 琼海市| 宜宾县| 朝阳市| 婺源县| 娄底市| 滁州市| 山东| 团风县| 游戏| 岑巩县| 蓬莱市| 太保市| 镇坪县| 土默特右旗| 宜都市| 咸阳市| 广德县| 石林| 大邑县| 保德县| 玉门市| 长乐市| 十堰市| 秦皇岛市| 成武县| 田林县| 泗水县| 河源市| 霍林郭勒市|