Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>+=</code> operator of String in fact create a new instance of a String instead of modifying your original string object. In other words, the following two lines of code are equivalent:</p> <pre><code>s += "Gotcha" s = new String(s + "Gotcha"); </code></pre> <hr> <p><em><strong>Example</em></strong></p> <p>Below is an example doing something similar to your <code>doSomeThing</code></p> <pre><code>public class st { public static void main(String[] args) { String hello = "hello"; String helloReference = hello; // making helloReference and hello refer to same object. helloReference += " world"; // perform += System.out.format("`hello` = %s, `helloReference` = %s\n", hello, helloReference); } } </code></pre> <p>and its output is the following, which shows that the object referred by <code>hello</code> is not affected by the <code>+=</code> operator performed by <code>helloReference</code>:</p> <pre><code>hello = `hello`, helloReference = `hello world` </code></pre> <p>In other world, before the += operator:</p> <pre><code> hello helloReference | | ------------------------------- | "hello" | ------------------------------- </code></pre> <p>after the += operator, it will create an new instance and modify the object which <code>helloReference</code> referenced to:</p> <pre><code> hello helloReference | | -------------------- | --------------| "hello world" | ------------------------------- -------------------- | "hello" | ------------------------------- </code></pre> <hr> <p>So your code is safe to go, the action performed in you doSomeThing will affect the objects referred by your SomeClass:</p> <pre><code>public class DoSomething { for (String s : SomeClass.getSomeList()){ // this create an new string instance to s s+="Gotcha"; } } </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. 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