aspose file tools*
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
Author

regarding operators

rameshbabu yarlagadda
Greenhorn

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

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


[code]
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: http://aspose.com/file-tools
 
subject: regarding operators