title: Is it possible to create a new String from a subset of another String without any data copy?
body: <p>I had (apparenty an incorrect?) impression that Java substring(srcArray, startIndex, endIndex) method did not allocate new memory but instead re-used the existing underlying char[] array.  This approach would seem to be possible due to the immutability of String's.</p>
  
  <p>However when actually looking at the JDK source we find the following:</p>
  
  <pre><code>public String substring(int beginIndex, int endIndex) {
  
      return ((beginIndex == 0) &amp;&amp; (endIndex == value.length)) ? this
              : new String(value, beginIndex, subLen);
  }
  
  public String(char value[], int offset, int count) {
  
  
      this.value = Arrays.copyOfRange(value, offset, offset+count);
  
  }
  </code></pre>
  
  <p>Notice the copyOfRange which copies/duplicates the content.</p>
  
  <p>The motivation for this was working with very large arrays representing the backing store of matrices for linear algebra. There is NO way we are going to accept copying them.  I would like however to have a custom representation of portions of the backing array for convenience in programming. It would however only be reasonable if no data copy were to be performed.  The tie-in with String is that I looked into how the String handles no-data-copy and found that they (in later java versions apparently..) do actually perform the copy.</p>
  
  <p>So, is there actually any way to achieve the zero-copy?</p>