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 regarding operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "regarding operators" Watch "regarding operators" New topic

regarding operators

rameshbabu yarlagadda

Joined: Jul 15, 2007
Posts: 10
what is the Difference between == operator and .equals(),=.
Where can we use above things.
Acoyani Garrido Sandoval

Joined: Sep 09, 2007
Posts: 3
As far as I know, the == works with everything but strings. That's why you have the .equals(Some string) method: because this way, you can see if a string is the same as another one.

I don't know if you can use this same method with other data types though. However, I'd rather use the plain ol' ==, because that means "compare", while the .equals() means "go call a function that compares and returns a boolean value", which is of course much, much less efficient.
[ September 09, 2007: Message edited by: Acoyani Garrido Sandoval ]
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
if you want to compare two primitive types then you'll use == operator and for all other objects (String, vector, set etc) you'll use .equal.

.equal method compare the contents of the contained by the object. if you use == operator on two objects (say string) it'll compare the memory location not the string itself.

just look at the following code

in this case the if condition will work

now look at the following code .. and test yourself

String a ="a";
String b = new String("a");
if (a==b){
System.out.println("the else statement will be printed, find out why");

The difference between <b>failure</b> and <b>success</b> is often being <b>right</b> and being <b>exactly right</b>.
I agree. Here's the link:
subject: regarding operators
It's not a secret anymore!