• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

euals and ==

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am too confused between equals and ==.
Can someone explain to me the difference between them. I understand how String works with them, but i am not sure of others. Does all the wrapper class works the way String does?? Could you please help me in getting this clarified.

Thanks
Sowmya
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

equals() method in "wrapper classes for primitive data types" also work in the same way as Strings.

Comparing two different wrapper types results in false.

Ex:

class Sample
{
public static void main(String[] args)
{
Integer i1=new Integer(4);
Integer i2=new Integer(4);
Long l1=new Long(4);
Long l2=new Long(4);

System.out.println(i1.equals(i2)); // returns true.
System.out.println(l1.equals(l2)); // returns true.
System.out.println(i1.equals(l1)); // returns false.
}
}

Regards,
Surekha.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The == operator compares values. Applied to primitives like int or boolean, this is straight forward. But applied to object variables, this operator simply compares references (i.e., memory addresses). Therefore, under the == operator, two object variables are "equal" only if they reference the same instance.

In contrast, the equals method is intended for a deeper comparison of instances. In the base class of Object, equals is implemented in the same manner as the == operator. However, in any other class, the equals method should be overridden to provide a meaningful result. What exactly it means for two different instances to be "equal" depends on the class.

For example, suppose you have a class called MacComputer. Two different instances of MacComputer might be considered "equal" if they are both 12" PowerBooks with a 1.5 GHz G4 processor and 512 MB of RAM. In this example, the equals method in the class MacComputer would be overridden to compare 3 fields: model (PowerBook12), processor (G4_1.5), and ram (512). It's up to the programmer to decide what exactly "equals" means for any particular class.

Many (not all) classes in the Java API have an overridden equals method. For classes that do not override equals (or are not derived from any superclass that overrides equals), the default implementation of Object is invoked -- a simple comparision of references. To see how equals is implemented in a particular class of the API, check the documentation.
[ November 12, 2005: Message edited by: marc weber ]
 
Sowmya Venkataraman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marc and Surekha!
 
ice is for people that are not already cool. Chill with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic