3

In this example I thought that the result was true. I thought that the variable stored in the string pool. The answer was: returns false because the two String objects are not the same in memory. One comes directly from the string pool and the other comes from building using String operations.

String a = "";
 a += 2;
 a += 'c';
 a += false;
 if ( a == "2cfalse") System.out.println("==");

I do not understand where the variable a was stored

Neithblue
  • 41
  • 3
  • I don't think there is any guarantee that a specific string will be pulled from the string pool. – forgivenson Nov 09 '15 at 14:36
  • 1
    @cricket_007: No, absolutely not. It's not asking anything about that. It's asking about whether or not `a` is interned. – Makoto Nov 09 '15 at 14:36
  • Related: http://stackoverflow.com/q/8914878/1065197 http://stackoverflow.com/q/21850156/1065197 http://stackoverflow.com/q/21676143/1065197 http://stackoverflow.com/q/14150628/1065197 and on... – Luiggi Mendoza Nov 09 '15 at 14:38
  • @Makato - Still, the answer if false for reasons discussed in that post – OneCricketeer Nov 09 '15 at 14:38
  • @cricket_007: I am ***well*** aware as to why the result is `false`, but I maintain that *isn't* the point of this question. – Makoto Nov 09 '15 at 14:38
  • Great, let's continue upvoting a duplicate Q/A... – Luiggi Mendoza Nov 09 '15 at 14:40
  • @LuiggiMendoza: To be blunt, I'm enjoying the answer posed here more so than I am the other one. It explains the scenario a fair bit clearer. Even if they are dupes, that doesn't make this question *bad*. – Makoto Nov 09 '15 at 14:44
  • @LuiggiMendoza I do not agree to the duplicate flag, so yes, I will continue upvoting. The question you marked this as a duplicate of does not go in depth about the workings of a string pool when using string operators, this one does. – Daniël van den Berg Nov 09 '15 at 14:45
  • @DaniëlvandenBerg Check the last sentence of the accepted answer. `then while a String will exist with the value test, said String will not exist in the pool as the literal "test" has never occurred.`. Actually that is fullfully answering the question here. – SomeJavaGuy Nov 09 '15 at 14:49
  • @DaniëlvandenBerg I disagree with you too. The accepted answer covers this clearly in this part: *Strings are only put in the pool when they are interned explicitly or by the class's use of a literal. So if you have, for example, this scenario `String test = new String(te) + new String(st)`* and OP's piece of code doesn't belong to a literal string but to the concatenation of literal strings, thus not being stored in the string pool. – Luiggi Mendoza Nov 09 '15 at 14:49

1 Answers1

3

Okay, so two responses to this. First, the ethically correct one, do never test strings with ==, always use .equals() or .equalsIgnoreCase().

Secondly, it's true that indeed, "a" == "a" because the strings are stored in, as you call it, the same pool. The problem here is that you append to it. Appending to a string causes it to become a different string, which is not stored in the string pool. The string pool is only generated on-compile, and as the second string is calculated on runtime, it won't match the one generated on-compile.

Imagine a string-pool to work like this:

a = "test";
b = "te";
c = "st";
d = "test";

The compiler translates this into

sp1 = "test";
sp2 = "te";
sp3 = "st";
a = sp1;
b = sp2;
c = sp3;
d = sp1;

Now == will check if two variables refer to the same sp. If you run b + c java will not go back and check if any of the sp's is the same as that. It only does that on compile.

Daniël van den Berg
  • 2,197
  • 1
  • 20
  • 45