Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Strings in Java are immutable</strong>. Basically this means that, once you create a string object, you won't be able to modify/change the content of a string. As a result, if you perform any manipulation on a string object which "appears to" change the content of the string, Java creates a new string object, and performs the manipulation on the newly created one.</p> <p>Based on this, your code above appears to create five string objects - two are created by the declaration, two are created by calls to <code>substring</code>, and the last one is created after you concatenate the two pieces.</p> <p>Immutability however leads to another interesting consequence. JVM internally maintains something like a string pool for creating string literals. For saving up memory, JVM will try to use string objects from this pool. Whenever you create a new string literal, JVM will loop into the pool to see if any existing strings can be used. If there is, JVM will simply use it and return it.</p> <p>So, technically, before Java 7, JVM will create only one string object for your whole code. Even your <code>substring</code> calls won't create new string objects in the pool, it will use the existing "Hello World" one, but in this case it will only use characters from position 0 to 3 for your first call to substring, for example. Starting from Java 7, substring will not share the characters, but will create a new one. So, total object count will be 4 - the last one will be created with the concatenation of the two substrings.</p> <p><strong>Edit</strong> To answer your question in the comment, take a look at <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.9" rel="nofollow">Java Language Specification</a> - </p> <blockquote> <p>In the Java programming language, unlike C, an array of char is not a String, and neither a String nor an array of char is terminated by '\u0000' (the NUL character).</p> <p>A String object is immutable, that is, its contents never change, while an array of char has mutable elements.</p> <p>The method toCharArray in class String returns an array of characters containing the same character sequence as a String. The class StringBuffer implements useful methods on mutable arrays of characters.</p> </blockquote> <p>So, no, char arrays are not immutable in Java, they are mutable.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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