Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Split does not create brand new strings, it uses <code>substring</code> internally which creates a new <code>String</code> object that points to the right substring of the original string, without copying the underlying <code>char[]</code>.</p> <p>So apart from the (slight) overhead of object creation, it should not have a huge impact from a memory perspective.</p> <p>ps: <code>StringTokenizer</code> uses the same technique so it would probably yield the same results as split.</p> <p><strong>EDIT</strong> </p> <p>To see that it is the case, you can use the sample code below. It splits <code>abc,def</code> into <code>abc</code> and <code>def</code> then prints the underlying <code>char[]</code> of the original string and of the split strings - the output shows that they are all the same.</p> <p>Output:</p> <pre class="lang-none prettyprint-override"><code>Reference: [C@3590ed52 Content: [a, b, c, ,, d, e, f] Reference: [C@3590ed52 Content: [a, b, c, ,, d, e, f] Reference: [C@3590ed52 Content: [a, b, c, ,, d, e, f] </code></pre> <p>Code:</p> <pre><code>public static void main(String[] args) throws InterruptedException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { String s = "abc,def"; String[] ss = s.split(","); Field f = String.class.getDeclaredField("value"); f.setAccessible(true); System.out.println("Reference: " + f.get(s) + "\tContent: " + Arrays.toString((char[])f.get(s))); System.out.println("Reference: " + f.get(ss[0]) + "\tContent: " + Arrays.toString((char[])f.get(ss[0]))); System.out.println("Reference: " + f.get(ss[1]) + "\tContent: " + Arrays.toString((char[])f.get(ss[1]))); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload