aspose file tools*
The moose likes Beginning Java and the fly likes How to compare? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to compare?" Watch "How to compare?" New topic
Author

How to compare?

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
I'm confused. I hope you can help me....when should i use the method equals() and when should i use the == operator?


Be Afraid...Be very Afraid...
Drake Silver
Ranch Hand

Joined: Apr 21, 2002
Posts: 80
.equals() is when comparing strings.
Like say,
var
total = 10;
then
if(textFeild.getText().equals("10"))
do something...
or
if(string1.equals(string2))
do something...
equals() is used on strings.


"Shh, Zog....here come one Now!"
Drake Silver
Ranch Hand

Joined: Apr 21, 2002
Posts: 80
since java is case sensitive, it makes sense to test for multiple varitations of an input.
ex:
prompt: Enter Yes to continue.
User input may be:
yes, or Yes, or YES...or a few other ways.
Instead of testing for each different variation, you can use equalsIgnoreCase().
So:
prompt: Enter yes to continue.
if(textfield.getText().equalsIgnoreCase(string))
do something...
Just thought I'd add that, since up until recently I didn't know about it.
Jose Velarde
Ranch Hand

Joined: Sep 11, 2002
Posts: 78
Simply put, you use == (1)for testing for equality of value of primitives, or (2)for testing whether two object references point to the same location in memory. On the other hand, the equals() method applies whatever test for equality the programmer provides.
For instance, in java.lang.String, equals() tests whether two String objects have the same content. However, in java.lang.Object, equals() actually is the same as == for objects.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to compare?