Java - Automatic String interning within constructors
<p>Let's say I have a class as follows:</p>

<pre><code>class Apple {

    String apple;

    Apple(String apple) {
        this.apple = apple;
    }
}
</code></pre>

<p>What makes the following code true?</p>

<pre><code>public boolean result() {
    Apple a = new Apple("apple");
    Apple b = new Apple("apple");

    return a.apple == b.apple;
}
</code></pre>

<p>Does Java automatically intern Strings set within instances of my objects?</p>

<p>Is the only time that Java doesn't intern Strings is when they're created using <code>new String("...")</code>?</p>

<p><strong>EDIT</strong>:</p>

<p>Thanks for the answers, an extension to this question would then be to say </p>

<pre><code>Apple a = new Apple(new String("apple"));
Apple b = new Apple(new String("apple"));
</code></pre>

<p>returns <strong>false</strong> with the same test.</p>

<p>This is because I am passing an instance of String into the constructor as opposed to a String literal.</p>