how the subString() function of string class works

后端 未结 7 602
后悔当初
后悔当初 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: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

提交回复
热议问题