最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年05月01日 10:36:41   作者:Hello_xzy_World  
這篇文章主要介紹了Java substring方法實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

substring實(shí)現(xiàn)原理

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

substring(int beginIndex, int endIndex)方法在不同版本的JDK中的實(shí)現(xiàn)是不同的。了解他們的區(qū)別可以幫助你更好的使用他。為簡(jiǎn)單起見,后文中用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時(shí)發(fā)生了什么?

你可能知道,因?yàn)閤是不可變的,當(dāng)使用x.substring(1,3)對(duì)x賦值的時(shí)候,它會(huì)指向一個(gè)全新的字符串:

然而,這個(gè)圖不是完全正確的表示堆中發(fā)生的事情。因?yàn)樵趈dk6 和 jdk7中調(diào)用substring時(shí)發(fā)生的事情并不一樣。

JDK 6中的subString

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

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

源碼

//JDK 6
String(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);
}

存在的問題

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

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

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

JDK 7 中的substring

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

源碼

  //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);
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringMVC+MyBatis 事務(wù)管理(實(shí)例)

    SpringMVC+MyBatis 事務(wù)管理(實(shí)例)

    本文先分析編程式注解事務(wù)和基于注解的聲明式事務(wù)。對(duì)SpringMVC+MyBatis 事務(wù)管理的相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2017-08-08
  • Javaweb實(shí)現(xiàn)郵件發(fā)送

    Javaweb實(shí)現(xiàn)郵件發(fā)送

    這篇文章主要為大家詳細(xì)介紹了Javaweb實(shí)現(xiàn)郵件發(fā)送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • java數(shù)組、泛型、集合在多態(tài)中的使用及對(duì)比

    java數(shù)組、泛型、集合在多態(tài)中的使用及對(duì)比

    本文主要介紹了java數(shù)組、泛型、集合在多態(tài)中的使用及對(duì)比。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-03-03
  • 如何使用BufferedReader循環(huán)讀文件

    如何使用BufferedReader循環(huán)讀文件

    這篇文章主要介紹了如何使用BufferedReader循環(huán)讀文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 一篇文章帶你詳解Spring的概述

    一篇文章帶你詳解Spring的概述

    這篇文章主要為大家介紹了Spring的概述,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • 排查Java應(yīng)用內(nèi)存泄漏問題的步驟

    排查Java應(yīng)用內(nèi)存泄漏問題的步驟

    這篇文章主要介紹了排查Java應(yīng)用內(nèi)存泄漏問題的步驟,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-11-11
  • java編程實(shí)現(xiàn)根據(jù)EXCEL列名求其索引的方法

    java編程實(shí)現(xiàn)根據(jù)EXCEL列名求其索引的方法

    這篇文章主要介紹了java編程實(shí)現(xiàn)根據(jù)EXCEL列名求其索引的方法,涉及Java元素遍歷與數(shù)學(xué)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • java安全?ysoserial?CommonsCollections1示例解析

    java安全?ysoserial?CommonsCollections1示例解析

    這篇文章主要介紹了java安全?ysoserial?CommonsCollections1示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Java怎樣判斷堆區(qū)中的對(duì)象可以被回收了

    Java怎樣判斷堆區(qū)中的對(duì)象可以被回收了

    文章介紹了Java垃圾回收機(jī)制的工作原理,主要通過引用計(jì)數(shù)法和可達(dá)性分析法來判斷對(duì)象是否可以被回收,引用計(jì)數(shù)法存在循環(huán)引用問題,而可達(dá)性分析法則使用GCRoot對(duì)象來判斷對(duì)象是否可達(dá),從而決定是否回收,這兩種方法各有優(yōu)缺點(diǎn),但Java最終采用了可達(dá)性分析法來實(shí)現(xiàn)垃圾回收
    2024-12-12
  • java使用CompletableFuture分批處理任務(wù)實(shí)現(xiàn)

    java使用CompletableFuture分批處理任務(wù)實(shí)現(xiàn)

    本文主要介紹了java使用CompletableFuture分批處理任務(wù)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07

最新評(píng)論

基隆市| 吉安市| 永修县| 万盛区| 桦甸市| 长宁县| 玉山县| 高阳县| 大足县| 鄱阳县| 南平市| 平潭县| 堆龙德庆县| 徐水县| 高阳县| 保山市| 木兰县| 临江市| 古浪县| 祥云县| 皋兰县| 同心县| 北辰区| 孝昌县| 孝义市| 阿克苏市| 兴国县| 上犹县| 香河县| 沙洋县| 平乐县| 临夏县| 堆龙德庆县| 乐都县| 新民市| 运城市| 绥宁县| 贺州市| 桂阳县| 喜德县| 深州市|