Method Pointers

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


On Mon, Mar 26, 2012 at 6:20 AM, Brian Goetz <brian.goetz at oracle.com> 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
edition)<http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5>
(quoted
below) describes this behavior in enough detail that I think it is fair to
say it is a language feature.

Cheers,
Neal


==============================

A string literal is a reference to an instance of class String
(§4.3.1<http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.3.1>
, §4.3.3<http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-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
(§15.28<http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28>)
- are "interned" so as to share unique instances, using the method
String.intern.

*Example 3.10.5-1. String Literals*

The program consisting of the compilation unit
(§7.3<http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.3>
):


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
(§8<http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html>)
   in the same package
(§7<http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html>)
   represent references to the same String object
(§4.3.1<http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.3.1>
   ).
   -

   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
(§15.28<http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28>)
   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