2

I have a simple application with a main method that compares two literal strings:

public class App {
    public static void main (String[] args) {
        String first = "a";
        String second = "a";
        System.out.println(first == second);
    }
}

When I run this program in Eclipse, I get false as result. But when I compile and run this program by the command line, I get true.

The result should be true for both. Why does Eclipse return false?

I have tested it using JDK 7 and 8.


Note that I am aware that comparison with == compares for identity.

The JLS requires "a" to come out of the internal String cache, for both variables.

String first = "a";  // Creates instance, stores in cache and returns
String second = "a"; // Should return instance from cache

However, if I run this from Eclipse I get false for the comparison, so both instances seem to be different although the JLS requires them to be the same.

Therefore, see JLS§3.10.5 String Literals:

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) - are "interned" so as to share unique instances, using the method String.intern.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Lucas Yoshioka
  • 87
  • 1
  • 1
  • 4
  • 4
    Do not compare object types with `==`. Simple as that. – Elliott Frisch Jul 20 '18 at 00:44
  • That code is required by the language specification to print `true`, so I don't think that's the code you're running when it prints `false`. – Radiodef Jul 20 '18 at 00:46
  • 3
    I'm trying to figure out why I got different results for the same application depending on how I compile and run. – Lucas Yoshioka Jul 20 '18 at 00:47
  • 2
    Not sure why people are down-voting. The question seems clear to me. Not nice to scare off new people without so much as a reason why. – CodeMonkey Jul 20 '18 at 00:50
  • 1
    I know that == compares references. But that's not my question. I actually don't know why people are down voting if they also don't know the answer. – Lucas Yoshioka Jul 20 '18 at 00:54
  • 1
    order by up vote 0 down vote Maybe you should reformulate the question emphasazing you wanna know about the difference between compilers, not about object comparison – Jaumzera Jul 20 '18 at 01:03
  • It cannot print false. Because jdk will refer to same location where it stores the literal `"a"`. – Pritam Banerjee Jul 20 '18 at 01:04
  • @Phil I can't reproduce that. In fact, IntelliJ gives me a warning that `a == b` is always `true`. – Radiodef Jul 20 '18 at 01:07
  • @Jaumzera how could OP have made their question any clearer? The title and text of the question clearly states they're getting different results and the question is literally _"Why?"_ – Phil Jul 20 '18 at 01:08
  • @Phil Report it as a bug, then. ["Moreover, a string literal always refers to the same instance of class `String`."](https://docs.oracle.com/javase/specs/jls/se10/html/jls-3.html#jls-3.10.5) – Radiodef Jul 20 '18 at 01:12
  • 1
    @Radiodef never mind, I was playing with the values and didn't notice I'd made them different. Chalk it up to end-of-week madness – Phil Jul 20 '18 at 01:14
  • I hope the edit made it clearer, voting for reopen. @LucasYoshioka You can avoid a situation like this by highlighting differences, stating your intend more explicit, elaborating the question. Still, the question does not contain enough information to reproduce the issue as some already mentioned they can't reproduce this with their Eclipse. Please add your exact eclipse version and possibly also plugins. – Zabuzard Jul 20 '18 at 14:03
  • When you get a different behavior for different compilers, you should include exact version numbers for the compilers. It may be a bug which is unlikely to be present in all versions of the compiler. – Holger Jul 31 '18 at 14:20

0 Answers0