473,466 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

equals() and =

73 New Member
can anyone explain about the equals method in Object class....and how it has been overriden in String class and also the difference between equals() and ==
Apr 23 '07 #1
3 1581
teddarr
143 New Member
The equals( ) method compares two objects for equality and returns 'true' if they are equal and 'false' otherwise. The method takes any object as an argument. When objects of a particular class must be compared for equality, the class should override the equals method to compare the contents of the two objects.

The method's use should meet these requirements:

1.) It should return 'false' if the argument is null.
2.)It should return 'true' if an object is compared to itself, as in object1.equals(objects1).
3.)It should return 'true' only if both object1.equals(object2) and objects2.equals(object1) would be true.
4.)For three objects, if object1.equals(object2) returns 'true' and object2.equals(object3) returns 'true' then object1.equals(object3) should also return 'true'.

Here is an implementation of equals for a data-centric class. It demonstrates how different types of fields are treated:

object fields, including collections : use equals
type-safe enumerations : use either equals or == (they amount to the same thing, in this case)
possibly-null object fields : use both == and equals
array fields : use Arrays.equals
primitive fields other than float or double : use ==
float : convert to int using Float.floatToIntBits, then use ==
double : convert to long using Double.doubleToLongBits, then use ==
Apr 23 '07 #2
jyohere
73 New Member
The equals( ) method compares two objects for equality and returns 'true' if they are equal and 'false' otherwise. The method takes any object as an argument. When objects of a particular class must be compared for equality, the class should override the equals method to compare the contents of the two objects.

The method's use should meet these requirements:

1.) It should return 'false' if the argument is null.
2.)It should return 'true' if an object is compared to itself, as in object1.equals(objects1).
3.)It should return 'true' only if both object1.equals(object2) and objects2.equals(object1) would be true.
4.)For three objects, if object1.equals(object2) returns 'true' and object2.equals(object3) returns 'true' then object1.equals(object3) should also return 'true'.

Here is an implementation of equals for a data-centric class. It demonstrates how different types of fields are treated:

object fields, including collections : use equals
type-safe enumerations : use either equals or == (they amount to the same thing, in this case)
possibly-null object fields : use both == and equals
array fields : use Arrays.equals
primitive fields other than float or double : use ==
float : convert to int using Float.floatToIntBits, then use ==
double : convert to long using Double.doubleToLongBits, then use ==
Sorry...i Did not get the difference between equals() and ==....
Apr 23 '07 #3
r035198x
13,262 MVP
Sorry...i Did not get the difference between equals() and ==....
Think of it as == compares memory locations (references) and .equals compares the contents of objects
Apr 23 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Kurt | last post by:
Wouldn't you agree all of the follwoing should produce the same result? r = (o1 == o2); r = (o2 == o1); r = object.Equals(o1, o2); r = object.Equals(o2, o1); r = (o1.Equals(o2)); r =...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
7
by: =?Utf-8?B?QWxleCBDb2hu?= | last post by:
In C++, there is an easy technique to provide an overloaded Equals() method. A straightforward translation to C# causes a stack overflow. Why does b.Equals(ba) in the snippet below not understand...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.