Method Pointers

Neal Gafter neal at
Mon Mar 26 09:33:48 PDT 2012

On Mon, Mar 26, 2012 at 6:20 AM, Brian Goetz <brian.goetz at> wrote:

> We're getting pretty far afield here from lambda, but it is most
> certainly not a language feature.  The intent was never to be able to
> compare strings with ==.  (How could users know that the other string
> came from a literal?)
> Perhaps your confusion is one of terminology?  The fact that some part
> of the *platform* specifies a behavior does not mean that it is a
> language feature.

Section "3.10.5. String Literals" of the Java Language Specification (SE7
below) describes this behavior in enough detail that I think it is fair to
say it is a language feature.



A string literal is a reference to an instance of class String
, §4.3.3<>

Moreover, a string literal always refers to the *same* instance of class
String. This is because string literals - or, more generally, strings that
are the values of constant expressions
- are "interned" so as to share unique instances, using the method

*Example 3.10.5-1. String Literals*

The program consisting of the compilation unit

package testPackage;
class Test {
    public static void main(String[] args) {
        String hello = "Hello", lo = "lo";
        System.out.print((hello == "Hello") + " ");
        System.out.print((Other.hello == hello) + " ");
        System.out.print((other.Other.hello == hello) + " ");
        System.out.print((hello == ("Hel"+"lo")) + " ");
        System.out.print((hello == ("Hel"+lo)) + " ");
        System.out.println(hello == ("Hel"+lo).intern());
class Other { static String hello = "Hello"; }

and the compilation unit:

package other;
public class Other { public static String hello = "Hello"; }

produces the output:

true true true true false true

This example illustrates six points:


   Literal strings within the same class
   in the same package
   represent references to the same String object

   Literal strings within different classes in the same package represent
   references to the same String object.

   Literal strings within different classes in different packages likewise
   represent references to the same String object.

   Strings computed by constant expressions
   are computed at compile time and then treated as if they were literals.

   Strings computed by concatenation at run-time are newly created and
   therefore distinct.

   The result of explicitly interning a computed string is the same string
   as any pre-existing literal string with the same contents.

More information about the lambda-dev mailing list