• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

String and ==  RSS feed

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can someone tell me why the output differs in following two cases.



Here ans is Equal
and



Here ans is Not Equal


Thanks,
Vivian
 
Rancher
Posts: 43009
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparing Strings using "==" is a tricky business with a number of non-obvious complications. Do you really mean to do this, or is it actually the String.equals method you should be using?
 
Vivian Josh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question from one of the mock exam available on internet.

This is my understanding of the following code. Let me know if I am getting it wrong somewhere.




will return "Equal" because both "String" has no leading/trailing white spaces so same object (two references refering to same object) is returned from String Pool.

And now the second case:




If first one is true then it should also work the same way here too.

"String".toUpperCase() will return STRING and then another execution of the same thing should return reference to the same object from String object pool.

Isn't it true? Or it is not happening this way?

Any help will be appreciated.

Thanks,

Vivian
 
Ranch Hand
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the new String is not same as the old one (literally), a new String object is returned else the same object. You can look into the Source of java.lang.String.java.

is false.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to further elaborate on what has already been discussed:

System.out.println("String".toUpperCase() == "STRING"); // returns false

This will return false using the == operator. Basically the toUpperCase (or any String method) will create a new object in the string pool. So think of string methods as the equivalent of using the new String("String") constructor.

However, their are 2 exceptions to this rule (Don't you just love the complexities of Java, come on it's sooo much better than PHP). The first is when the intern() method is used. basically intern() condenses the string pool and unite all strings so that they are the same object. Good for memory management.

System.out.println("String".toUpperCase().intern() == "STRING"); //returns true

The next is when the method makes no changes to the string. In this case true is also returned.

System.out.println("STRING".toUpperCase().intern() == "STRING"); // returns true
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!