成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

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

Java substring方法實現(xiàn)原理解析

瀏覽:22日期:2022-09-02 10:42:55

substring實現(xiàn)原理

String是Java中一個比較基礎(chǔ)的類,每一個開發(fā)人員都會經(jīng)常接觸到。而且,String也是面試中經(jīng)常會考的知識點。String有很多方法,有些方法比較常用,有些方法不太常用。今天要介紹的subString就是一個比較常用的方法,而且圍繞subString也有很多面試題。

substring(int beginIndex, int endIndex)方法在不同版本的JDK中的實現(xiàn)是不同的。了解他們的區(qū)別可以幫助你更好的使用他。為簡單起見,后文中用substring()代表substring(int beginIndex, int endIndex)方法。

substring()的作用

substring(int beginIndex, int endIndex)方法截取字符串并返回其[beginIndex,endIndex-1]范圍內(nèi)的內(nèi)容。s

String x = 'abcdef';x = x.substring(1,3);System.out.println(x);

輸出內(nèi)容:

bc

調(diào)用substring時發(fā)生了什么?

你可能知道,因為x是不可變的,當(dāng)使用x.substring(1,3)對x賦值的時候,它會指向一個全新的字符串:

Java substring方法實現(xiàn)原理解析

然而,這個圖不是完全正確的表示堆中發(fā)生的事情。因為在jdk6 和 jdk7中調(diào)用substring時發(fā)生的事情并不一樣。

JDK 6中的subString

tring是通過字符數(shù)組實現(xiàn)的。在jdk 6 中,String類包含三個成員變量:char value[], int offset,int count,他們分別用來:存儲真正的字符數(shù)組、存儲數(shù)組的第一個位置索引、存儲字符串中包含的字符個數(shù)。

當(dāng)調(diào)用substring方法的時候,會創(chuàng)建一個新的string對象,但是這個string的值仍然指向堆中的同一個字符數(shù)組。這兩個對象中只有count和offset 的值是不同的。

Java substring方法實現(xiàn)原理解析

源碼

//JDK 6String(int offset, int count, char value[]) { this.value = value; this.offset = offset; this.count = count;}public String substring(int beginIndex, int endIndex) { //check boundary return new String(offset + beginIndex, endIndex - beginIndex, value);}

存在的問題

如果有一個很長的字符串,但是你只需要使用很短的一段,于是你使用substring進(jìn)行切割,但是由于你實際上引用了整個字符串,這個很長的字符串無法被回收。往小了說,造成了存儲空間的浪費,往大了說,可能造成內(nèi)存泄漏。這個問題已經(jīng)被官方記錄在Java Bug Database里面了:

Java substring方法實現(xiàn)原理解析

相應(yīng)的解決辦法:

s1 = s1.substring(x,y) + '';

JDK 7 中的substring

上述問題在JDK 7中得到了解決。JDK 7中,substring方法會在堆中創(chuàng)建一個新的數(shù)組。

Java substring方法實現(xiàn)原理解析

源碼

//JDK 7 /** * Allocates a new {@code String} that contains characters from a subarray * of the character array argument. The {@code offset} argument is the * index of the first character of the subarray and the {@code count} * argument specifies the length of the subarray. The contents of the * subarray are copied; subsequent modification of the character array does * not affect the newly created string. * * @param value Array that is the source of characters * @param offset The initial offset * @param count The length * @throws IndexOutOfBoundsException If the {@code offset} and {@code count} arguments index * characters outside the bounds of the {@code value} array */ public String(char value[], int offset, int count) { //check boundary this.value = Arrays.copyOfRange(value, offset, offset + count); } /** * Returns a string that is a substring of this string. The * substring begins at the specified {@code beginIndex} and * extends to the character at index {@code endIndex - 1}. * Thus the length of the substring is {@code endIndex-beginIndex}. * <p> * Examples: * <blockquote><pre> * 'hamburger'.substring(4, 8) returns 'urge' * 'smiles'.substring(1, 5) returns 'mile' * </pre></blockquote> * * @param beginIndex the beginning index, inclusive. * @param endIndex the ending index, exclusive. * @return the specified substring. * @throws IndexOutOfBoundsException if the * {@code beginIndex} is negative, or * {@code endIndex} is larger than the length of * this {@code String} object, or * {@code beginIndex} is larger than * {@code endIndex}. */ public String substring(int beginIndex, int endIndex) { //check boundary int subLen = endIndex - beginIndex; return ((beginIndex == 0) && (endIndex == value.length)) ?this :new String(value, beginIndex, subLen); }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 一本色道久久99一综合 | 一级成人a免费视频 | 国产三级精品美女三级 | 欧美黄色精品 | 中文字幕高清在线天堂网 | 日韩影院久久 | 亚洲综合伦理一区 | 在线看国产 | 成人自拍视频网站 | 午夜视频在线观看一区二区 | 免费视频久久看 | 久久亚洲国产午夜精品理论片 | 国产综合视频在线观看一区 | 999国产精品亚洲77777 | 国产三级a三级三级午夜 | 午夜爽爽| 国产做a爰片久久毛片a | 91精品国产手机 | 一区二区三区高清在线 | 老司机亚洲精品影院 | 99av在线播放| 欧美a一级片 | 午夜在线亚洲男人午在线 | 加勒比在线免费视频 | 久久不见久久见免费影院 | 亚洲国产成人久久综合一 | 国产乱淫视频 | 亚洲精品国产免费 | 亚洲一区二区三区中文字幕 | 国产做国产爱免费视频 | 免费观看毛片视频 | 亚洲线精品一区二区三区 | 一级特黄特黄的大片免费 | 中文字幕成人免费高清在线 | 国产日产欧美精品一区二区三区 | aaaaaaa一级毛片 | 欧美精品一级毛片 | 欧美国产综合视频 | 欧美成人鲁丝片在线观看 | 毛片在线播放网站 | 精品免费久久久久久久 |