how the subString() function of string class works

后端 未结 7 488
后悔当初
后悔当初 2020-12-07 04:47

please see the following code.

String s = \"Monday\";
if(s.subString(0,3).equals(\"Mon\"){}

String s2 = new String(s.subString(0,3));
String s3 = s.subStrin         


        
相关标签:
7条回答
  • 2020-12-07 05:26

    Read this https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html

    "Returns a new string..."

    0 讨论(0)
  • 2020-12-07 05:29

    At line 5---->s3=Mon .

    0 讨论(0)
  • 2020-12-07 05:32

    “Substring creates a new object out of source string by taking a portion of original string”.

    Until Java 1.7, substring holds the reference of the original character array, which means even a sub-string of 5 characters long, can prevent 1GB character array from garbage collection, by holding a strong reference.

    This issue is fixed in Java 1.7, where original character array is not referenced anymore, but that change also made the creation of substring bit costly in terms of time. Earlier it was in the range of O(1), which could be O(n) in worst case on Java 7.

    0 讨论(0)
  • 2020-12-07 05:34

    Note: As of Java 7 update 6 in Sun/Oracle's Java, it is no longer true that a String created by String.substring shares the parent's char array. It was decided that this optimization was rarely beneficial, and did not justify the cost and complexity of the offset and count fields.

    Some links:

    • Rationale: http://mail.openjdk.java.net/pipermail/core-libs-dev/2012-May/010257.html
    • Formal bug report: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6924259
    • String.java diff: http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/diff/e1c679a00712/src/share/classes/java/lang/String.java
    0 讨论(0)
  • 2020-12-07 05:34

    In Sun's implementation String objects have a private final char value[] field. When you create a new String by calling substring(), no new char array is created, the new instance uses the value of the original object. This is the case in line 2 and 5, the new String objects will use the char array of s.

    The constructor String(String) creates a new char array in case of the string length being less than the total length of the char array value. So the String created in line 4 will use a new char array.

    You should have a look at the source code of the constructor public String(String original), it's really simple.

    0 讨论(0)
  • 2020-12-07 05:46

    I know that line 2 will still point to "Monday" and have a new String object with the offset and count set to 0,3.

    That is currently true of the Sun JRE implementation. I seem to recall that was not true of the Sun implementation in the past, and is not true of other implementations of the JVM. Do not rely on behaviour which is not specified. GNU classpath might copy the array (I can't remember off hand what ratio is uses to decide when to copy, but it does copy if the copy is a small enough fraction of the original, which turned one nice O(N) algorithm to O(N^2)).

    The line 4 will create a new String "Mon" in string pool and point to it.

    No, it creates a new string object in the heap, subject to the same garbage collection rules as any other object. Whether or not it shares the same underlying character array is implementation dependant. Do not rely on behaviour which is not specified.

    The String(String) constructor says:

    Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.

    The String(char[]) constructor says:

    Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.

    Following good OO principles, no method of String actually requires that it is implemented using a character array, so no part of the specification of String requires operations on an character array. Those operations which take an array as input specify that the contents of the array are copied to whatever internal storage is used in the String. A string could use UTF-8 or LZ compression internally and conform to the API.

    However, if your JVM doesn't make the small-ratio sub-string optimisation, then there's a chance that it does copy only the relevant portion when you use new String(String), so it's a case of trying it a seeing if it improves the memory use. Not everything which effects Java runtimes is defined by Java.

    To obtain a string in the string pool which is equal to a string, use the intern() method. This will either retrieve a string from the pool if one with the value already has been interned, or create a new string and put it in the pool. Note that pooled strings have different (again implementation dependent) garbage collection behaviour.

    0 讨论(0)
提交回复
热议问题