wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Equals Method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Equals Method." Watch "Equals Method." New topic
Author

Equals Method.

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
When 2 primitive array references are checked using the equals() method, when are they considered equal??



Returns false. Is it because Array is an object and hence uses the == operator when using equals() method?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
You are correct Abhi. Arrays are objects, and the equals method is not overridden to check for element-wise equality. You can use one of the utility methods in the Arrays utility class though. I think it is called deepEquals or something to that effect. You can check the API for it.

EDIT: Actually, deepEquals is just for reference variable arrays. For primitive arrays you would just use equals. There are a few overloaded versions of equals to compare pairs of arrays of every primitive type.


All code in my posts, unless a source is explicitly mentioned, is my own.
Alpesh Rathod
Ranch Hand

Joined: Jan 06, 2009
Posts: 41
Well Keep one thing in mind that when its dealing with objects always use ==,because == always checks for location and not content whereas equals checks for content.
String is the only class which is not initialized wth new...and hence if the instance is of type String then we always go for equals method.
I hope i have cleared the things properly..if not rectify if possible...


Thanks,
Alps
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Alpesh Rathod wrote:Well Keep one thing in mind that when its dealing with objects always use ==,because == always checks for location and not content whereas equals checks for content.
String is the only class which is not initialized wth new...and hence if the instance is of type String then we always go for equals method.
I hope i have cleared the things properly..if not rectify if possible...

Alpesh,

Actually, the equals method provided by Object internally uses the == operator, so unless a specific class overrides the method to check for logical equivalence, you will still be using ==. String is not the only class which is not initialized with new, the wrapper classes can be declared and initialized in one line using autoboxing without using the new keyword.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Yes Ruben is right here.

For value -128 to 127, if you do not use new than Number wrappers do not create new object, they will give you cached object.


SCJP 6
Alpesh Rathod
Ranch Hand

Joined: Jan 06, 2009
Posts: 41
Hey Thanks for your Info..
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
String is not the only class which is not initialized with new, the wrapper classes can be declared and initialized in one line using autoboxing without using the new keyword.



Please, can someone elaborate?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Abhi vijay wrote:
String is not the only class which is not initialized with new, the wrapper classes can be declared and initialized in one line using autoboxing without using the new keyword.



Please, can someone elaborate?

Sure, do this:
Integer i = 1; //Declare and instantiate a new Integer object
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Abhi vijay wrote:
String is not the only class which is not initialized with new, the wrapper classes can be declared and initialized in one line using autoboxing without using the new keyword.



Please, can someone elaborate?


They are trying to say that...
its not just String object which can be created without using new keyword( ex:- String s = "james"; )
Even Wrapper objects can be created, ( Integer i = 2; )


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


I dont understand why line1 is giving TRUE and line2 FALSE???
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Abhi, I think that line 1 is using autounboxing in the wrapper, and then applying the equality operator. The problem with line 2 is that the equals method of wrapper classes only returns true if the argument object is of the same type and holds the same value
as this object. In your case, you have an Integer and a Long, so equals must return false.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:

I dont understand why line1 is giving TRUE and line2 FALSE???




this line is using primitive comparison so, it is actually comparing 20=20.


this line calls Integer class's equals() method.
and in equals method thing is something like this:



so here l1 instanceof Integer fails.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks Punit.

Can you tell me where can I find this information.
equals(Object o){
if(!(o instanceof Integer))
return false;
....
....
}
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
you will find src.zip in your JAVA_HOME directory.

I am using eclipse.

In eclipse press ctrl and click on Integer word in your code, it will open a new Integer.class and you can see attach source button, just click on that button and give it the location of your src.zip file.

So java source code will be available in your eclipse, if you click on Integer word with ctrl button pressed you can see source code of Integer.class. You can use ctrl+o (o for onion) to find equals() method.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Equals Method.
 
Similar Threads
Equals Method
ARRAY doubt
passing array in to method...doubt
Autoboxing vs Unboxing