Difference between double equals, instanceof(), and equals() method in Java

Difference between double equals, instanceof(), and equals() method in Java
February 15, 2014
Java

== (double equals)

https://technicalconfessions.com/images/postimages/postimages/_237_1_double equals in java 7.png

Compares two object to determine if they

  • Have the same value
  • Are located within the same memory
You can see in this example that i1, i2, i3, and i4 all equal to 42. When you create a new Integer object instance, it will autobox the value you pass.

i1 and i2 are the same value, and reside within the same memory location... so this returns true
i2 and i3 also have the same value, though the new Integer instance is created in a separate memory allocation, so this returns false
i3 and i4 have the same same value, the i3 is a new integer instance, then it's referenced to i4 thus they both reside within the same memory. So this returns true.

https://technicalconfessions.com/images/postimages/postimages/_237_2_double equals with Integer values.png

The issue with double equals is that it's dependent on the length of the value you're comparing to. The example shows 2 comparisons. 42's and 420's... though both return back different results. The double equals only work when the integer values are less than 128.

NOTE:
Be careful though, string comparisons can be erratic.
You cannot compare if 2 different objects are the same (e.g. Compare an int and a string)


instanceof()

Checks if the object is an instance of a particular class. It's not bothered about the value associated to it. This is ideal for non-known object types. The compiler will know before hand that a string is an instanceof an Integer. It will simply not compile.

https://technicalconfessions.com/images/postimages/postimages/_237_4_equals method in java 7.png

In the example, you can see I have created a Animal, lion, and Sea lion classes. when I run a1 instanceof Animal, it's checking if the Sea Lion class is associated to the Animal class. Because they all extend and the Animal class is the superclass, this will return true. If I queried whether a Lion is an instanceof a Sea Lion, because Lion looks up and cannot find Sea Lion in its higher hierarchy, it will return false.


equals()

The equals just checks if the value are the same. It's not bothered on whether both objects remain within the same memory. You do have the additional privilege on overriding this method if you wanted so YOU can determine 'what constitutes to equals' rather than just the value, though that's in another blog

https://technicalconfessions.com/images/postimages/postimages/_237_6_instanceof method in java 7.png

You can see within the example, it doesn't matter if you create new instances (which will placed these 2 values within separate memories, it will only look at the value. The last check determines that the last values are false, thus returning false

About the author

Daniel is a Technical Manager with over 10 years of consulting expertise in the Identity and Access Management space.
Daniel has built from scratch this blog as well as technicalconfessions.com
Follow Daniel on twitter @nervouswiggles

Comments

Other Posts

AWS-PHP integration - Email not sent. SMTP Error: Could not authenticate.

phpsmtpaws

February 6, 2020
Created by: Daniel Redfern
AS I was migrating my environment into an S3 environment, I wanted to leverage off the SES services that AWS provide, more specifically, to leverage the off the SMTP functionality by sending an email via PHP
Read More...

SOLUTION: no headers files (.h) found in softwareserial - Arduino

Arduino

February 24, 2019
Created by: Daniel Redfern
The WeMos D1 is a ESP8266 WiFi based board is an extension to the current out-of-the-box library that comes with the Arduino installation. Because of this, you need to import in the libraries as well as acknowledging the specific board. This process is highly confusion with a number of different individuals talking about a number of different ways to integrate.
Read More...

NameID element must be present as part of the Subject in the Response message

ShibbolethSAML

August 7, 2018
Created by: Daniel Redfern
NameID element must be present as part of the Subject in the Response message, please enable it in the IDP configuration.
Read More...

HOW TO provision AD group membership from OpenIDM

OpenIDMICFAD-connector

June 15, 2018
Created by: Daniel Redfern
For what I see, there's not too many supportive documentations out there that will demonstrate how provision AD group membership with the ICF connector using OpenIDM. The use of the special ldapGroups attribute is not explained anywhere in the Integrators guides to to the date of this blog. This quick blog identifies the tasks required to provision AD group membership from OpenIDM to AD using the LDAP ICF connector. However this doesn't really explain what ldapGroups actually does and there's no real worked example of how to go from an Assignment to ldapGroups to an assigned group in AD. I wrote up a wiki article for my own reference: AD group memberships automatically to users This is just my view, others may disagree, but I think the implementation experience could be improved with some more documentation and a more detailed example here.
Read More...

ForgeRock OpenIDM - InvalidCredentialException: Remote framework key is invalid

ICFIDMOpenIDMOpenICF

November 8, 2017
Created by: Daniel Redfern
In the past, the similar error occurred though for the Oracle Identity Management solution. invalidcredentialexception remote framework key is invalid Because they all share the ICF connector framework, the error/solution would be the same.
Read More...

org.forgerock.script.exception.ScriptCompilationException: missing ; before statement

IDMsync.confforgerockopenidm

November 8, 2017
Created by: Daniel Redfern
org.forgerock.script.exception.ScriptCompilationException: missing ; before statement
Read More...

ForgeRock IDM - org.forgerock.script.exception.ScriptCompilationException: missing ; before statemen

OpenIDMsync.confForgeRock

September 17, 2017
Created by: Daniel Redfern
ForgeRock IDM - org.forgerock.script.exception.ScriptCompilationException: missing ; before statement
Read More...

Caused by: org.forgerock.json.resource.BadRequestException: Target does not support attribute groups

OpenIDMForgeRockICFConnector

September 17, 2017
Created by: Daniel Redfern
When performing the attempt of a reconciliation from ForgeRock IDM to Active Directory, I would get the following error
Read More...

ForgeRock OpenIDM - InvalidCredentialException: Remote framework key is invalid

OpenIDMForgeRockICFConnectorAD

September 17, 2017
Created by: Daniel Redfern
In the past, the similar error occurred though for the Oracle Identity Management solution. invalidcredentialexception remote framework key is invalid Because they all share the ICF connector framework, the error/solution would be the same.
Read More...

ERROR Caused by com.google.api.client.auth.oauth2.TokenResponseException 400 Bad Request - invalid_g

OpenIDMIDMGoogleGoogle-AppsICFreconciliation

September 12, 2017
Created by: Daniel Redfern
During the reconcilation from OpenIDM to the ICF google apps connector, the following error response would occur. ERROR Caused by com.google.api.client.auth.oauth2.TokenResponseException 400 Bad Request - invalid_grant
Read More...