Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding operators

 
rameshbabu yarlagadda
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the Difference between == operator and .equals(),=.
Where can we use above things.
 
Acoyani Garrido Sandoval
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic