• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regarding operators

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the Difference between == operator and .equals(),=.
Where can we use above things.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
}
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic