• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

String objects: equals() or == .... Does it really matter

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

When comparing string objects. If we use the == operator or the .equals method. Does it really matter.

I understand that == is testing if reference variables refer to the same object and that .equals() method tests if objects are meaningfully equivalent.

But since Java does not create multiple versions of the same string in the string pool, does it make a difference which technique we use for comparing strings?

Can anyone let me know of a situation where the == method would not work for a string.

Many Thanks
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it makes a great deal of difference because only String literals and Strings objects not already in the pool on which you call the intern() method are placed into the pool.

Consider this situation.



Also consider a situation where you ask the user to input a String and you want to compare the String to a certain value.
[ July 24, 2006: Message edited by: Keith Lynn ]
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But since Java does not create multiple versions of the same string in the string pool, does it make a difference which technique we use for comparing strings?

Can anyone let me know of a situation where the == method would not work for a string.



Java does create a new String object when you create it using new and its reference is different from reference of string literal object. So comparing them using == operator returns false. However equals() still returns true provided content same.

e.g.,



Naseem
 
d jones
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that Keith and Naseem,

Keith, what do you mean by

only String literals and Strings objects not already in the pool on which you call the intern() method are placed into the pool.



Also, do we need to know about the intern method for the SCJP 1.4 exam. It's not something I have come across in the K&B book???

Thanks again
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I don't think the intern() method is required knowledge on the exam.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
Yes, it makes a great deal of difference because only String literals and Strings objects not already in the pool on which you call the intern() method are placed into the pool.



I don't think call to intern() is required to place string literal object into the pool.

Java automatically interns String literals and places them in pool.

Jones you can try this code...




When the intern() method is invoked on a String, a lookup is performed on a table of interned Strings. If a String object with the same content is already in the table, a reference to the String in the table is returned. Otherwise, the String is added to the table and a reference to it is returned. The result is that after interning, all Strings with the same content will point to the same object.

Naseem
[ July 24, 2006: Message edited by: Naseem Khan ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this JavaRanch article, Strings, Literally by Corey McGlone.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!