File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

How to compare?

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 202
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,
total = 10;
do something...
do something...
equals() is used on strings.

"Shh, 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.
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().
prompt: Enter yes to continue.
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:
subject: How to compare?
It's not a secret anymore!