Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Literals

 
Sukhitha sunil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//In File Other.java

//In File Test.java


produces the output:

true true true true false true


i dont get d logic of line4 and 5 .
when i printed variable hello and ("Hel"+lo)  it gives same output::     Hello.   but when it compares (hello == ("Hel"+lo)line4  output is false.  what would be the reason?
and what about intern()?
help me please
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!
Sukhitha sunil wrote:when i printed variable hello and ("Hel"+lo)  it gives same output::     Hello.   but when it compares (hello == ("Hel"+lo)line4  output is false.  what would be the reason?
You might know here == compares object references so local variable hello refers to the String object got created by String literal "Hello" whose reference is in String literal pool.
"Hel" + lo is a String concatenation which creates new String object having value "Hello" so both have different references ergo line 4 returns false.

and what about intern()? 
If a String equal to this String ( the String object on which intern method is invoked )  present in String literal pool then It returns that String reference from String literal pool else adds this String to pool and return this newly added String's reference.

So on line 5 new String object having value "Hello" is created as like line 4 and on this String object intern() method is invoked and It finds a String having equal value "Hello" is already present in String literal pool ( whose reference is assigned to local variable hello ) then this method returns reference of String present in String literal pool so on line 5 both refer to the same String object which returns true.

Edit: Since you are new here, I added code tags around your code and formatted It properly. Always post code with proper indentation and UseCodeTags ( <--- Click here ).
See now It's more readable now

If you are preparing for OCA exam you don't need to know the intern method.


 
Sukhitha sunil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:Welcome to CodeRanch!


thank you so much...explanation was very helpful . next time i  will be careful with formatting
 
Anton Golovin
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Sukhitha,

Starting from some version of JVM, they started putting equivalent strings into like static variables or their equivalent of some sort, for efficiency's sake.

This means that for Strings, == is equal to equals semantically speaking, because the object locations are the same as well as to String content. Funny but true.

With best regards,

Anton.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic