• 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

Equals Method.

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Alpesh Rathod
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Thanks for your Info..
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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; )
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I dont understand why line1 is giving TRUE and line2 FALSE???
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic