• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt regarding String '==' check  RSS feed

 
Manu Somasekhar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a code snippet



I am getting the result as true and false. I don't understand why because both Strings pairs s1,s2 and s3,s4 are made from concatenations. Any help is appreciated.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The operator == checks for identity (is this the same object?) not the equality (does the object hold the same value?).
Read this.

If you make a String from concatenation containing only String literals, the compiler will merge them and make a part of a String pool. So the objects are the same and == returns true.
Any other concatenation (like String + int) occurs during runtime. Thus different objects are created.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:If you make a String from concatenation containing only String literals, the compiler will merge them and make a part of a String pool. So the objects are the same and == returns true.
Any other concatenation (like String + int) occurs during runtime. Thus different objects are created.

Are you sure about that ? Note that the output was true, false.

Strings are only concatenated at compile time if they are part of the same statement. And it appears that that is also true for mixtures of String and int literals.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Pawel Pawlowicz wrote:If you make a String from concatenation containing only String literals, the compiler will merge them and make a part of a String pool. So the objects are the same and == returns true.
Any other concatenation (like String + int) occurs during runtime. Thus different objects are created.

Are you sure about that ? Note that the output was true, false.


Yes you are right. I messed something up.
But one thing is true (if inversed ;) ). First concatenation occurs at compile time. The second at runtime.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Strings are only concatenated at compile time if they are part of the same statement. And it appears that that is also true for mixtures of String and int literals.


The concatenation of two compile time constants results in another compile time constant. Since "abc" and 3 are both literals (and hence, compile time constants), the result "abc3" is also a compile time constant. So, the string "abc3" is a compile time constant and is located in the string pool (the same instance as the "abc3" literal).

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Or basically, if you can keep everything as compile time constants, you can get everything done at compile time -- hence, in the string pool, and hence, get true as the result.



Henry
 
Manu Somasekhar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pawel,Joanne, Henry.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!