• Post Reply Bookmark Topic Watch Topic
  • New Topic

String equality question  RSS feed

 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question regarding String equality:

For example:

        

I cannot understand why a and "java" with == are equal..
If we wrote a.equals("java") it should be ok.
but in the first option are they the same object??

Untitled.jpg
[Thumbnail for Untitled.jpg]
 
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

All string literals (actually, all compile time constant strings) are placed into the string pool. There is only one copy of each of those strings.

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java compiler has a smart optimization: if you use the same string literal multiple times, as you are using the literal "java" on lines 2 and 16 in your code, then the compiler creates only one String object which is re-used in all the places where you use it.
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just go through this --->Strings, Literally by Corey McGlone will clear all your doubts about String.
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:The Java compiler has a smart optimization: if you use the same string literal multiple times, as you are using the literal "java" on lines 2 and 16 in your code, then the compiler creates only one String object which is re-used in all the places where you use it.



So, if I understand correctly,
if I wrote for example



there would be a possibility that :



???
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:
So, if I understand correctly,
if I wrote for example



there would be a possibility that :



???
Here only one String object gets created having value "Java" whose reference is added in String literal pool, so both a and b refer to the same String object. 

Since both a and b refer to the same object, a == b compares reference so returns true and .equals(...) compares contents so returns true.

I would request you to go through the link which will answer all your question,It's worth reading.
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just read the link !! All my questions were answered thank you !!
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one last question :P

In this example:


Why is this false?
The trim confuses me. Doesn't it first trims , and then saves it into y?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:
The trim confuses me. Doesn't it first trims , and then saves it into y?


The trim() method, and actually *all* the methods of the String class that returns a modified string, doesn't deal with the string pool. If the trim() method modifies the string parameter, it returns a newly instantiated string... Of course, since this is an implementation detail, this may be different in the future.

Henry
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all..... one last thing :

In this example:


I cannot understand clearly the == equality. In the String pool exist only Strings that are initialized in compile time??
When a String is created during runtime (like this case, or when using the word new) do they have a copy in the String Pool or not??
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With this code, yes, there will a String in the String pool because you have literal String on line 7.

To get a better idea of how Java handles String values, consider this code:

This business with the String pool is not something that you should bother with too much. The important thing to remember is that == should never be used to compare objects for logical equality. Unless you want to specifically check for reference equality, use equals() instead of == when comparing objects.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!