Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, Strings in Java are immutable. That is to say, the content pointed too by a string variable cannot be changed after it has been initialized. Using your own examples to illustrate:</p> <pre><code>String s=new String(); </code></pre> <p>In the above code, you have created a new, empty String and assigned it to the <em>s</em> variable.</p> <pre><code>s="abc"; </code></pre> <p>You have created another new String, this time with content <strong>"abc"</strong>. You have set the variable <em>s</em> to point to this new String. Your previous, empty string will get garbage collected at some point in the future.</p> <pre><code>s="xyz"; </code></pre> <p>Similar to above. You have created another new String, with content <strong>"xyz"</strong>, and set the variable <em>s</em> to point to it. The previous <strong>"abc"</strong> string will get garbage collected at some point in the future.</p> <p>Note that at no point did you actually modify the empty string to become <strong>"abc"</strong>, or modify the <strong>"abc"</strong> string to become <strong>"xyz"</strong>. All operations on String that concatenate, convert case, or otherwise appear to modify the String actually return a new String with the function results. To illustrate:</p> <pre><code>String s = new String("Hello"); String b = s.concat(" World"); System.out.println(s); // This will NOT produce 'Hello World' System.out.println(b); // Whereas, this will </code></pre> <p>As to the last part of your question, you would use the <strong><em>length()</em></strong> function to determine the 'size' of your string. Note that this is based on Unicode encoding, and might not be what you expect the length to be as defined in ANSI C.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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