• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting strings equal to each other

 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to check and see if two strings are equal to each other, and am pretty sure this code:

if (title == courseTitle)
{
aCourse.creditHours = credits;
}

wont work b/c of the ==. Is there another operator or metod that must be used when checking to see if the two string are equal?
thanks.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean equalStrings = strA.equals(strB);

In general, you should always use the (overridden) equals method when comparing objects for "equality." The == is a simple comparison of values (i.e., references in the case of objects).

But you should also be aware that Strings are a special case because of a "String pool." For details, see Corey McGlone's article, "Strings, Literally."
[ March 16, 2006: Message edited by: marc weber ]
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this.


 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does equals refer to? I think, its an Object class method, and it checks for the hashcode value. Correct me, if something is amiss.
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aum Tao:
What does equals refer to? I think, its an Object class method, and it checks for the hashcode value. Correct me, if something is amiss.


No... In this case, the topic is about strings.

The String class overrides the equals() method, with a different implementation. The String classes' implementation actually checks the two strings character by character to see if they are equal by value.

Henry
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aum Tao:
What does equals refer to? I think, its an Object class method, and it checks for the hashcode value. Correct me, if something is amiss.

The equals method is defined in the base class Object, which guarantees that all objects have that method. In Object, the method's implementation is a simple comparison of references (as it must be to compare objects). However, the intent is for other classes to override the equals method to provide a comparison that is meaningful to that class.

As Henry pointed out above, String has overridden equals to compare Strings on a char by char basis. As described in the API documentation...
The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic