dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: What’s the difference between equals() and ==?

  1. #1
    Join Date
    Sep 2016
    Location
    Bhopal , Madhya pradesh
    Posts
    15

    What’s the difference between equals() and ==?

    What’s the difference between equals() and ==?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,420

    Re: What’s the difference between equals() and ==?

    The equals() method compares two objects for equality and returns true if they are equal. The implicit contract of the equals() method is that it tests for equality rather than identity. Thus most classes will override equals() with a version that does field by field comparisons before deciding whether to return true or false. An object created by a clone() method (that is a copy of the object) should pass the equals() test if neither the original nor the clone has changed since the clone was created. However the clone will fail to be == to the original object.

    See
    http://www.leepoint.net/data/express...reobjects.html
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.4)

  3. #3
    Join Date
    Oct 2017
    Posts
    5

    Re: What’s the difference between equals() and ==?

    You can refer below resource for difference between equals() and == with some example,

    http://www.flowerbrackets.com/string...-java-program/
    Last edited by sidsomashak; June 15th, 2018 at 03:20 AM. Reason: spelling mistake

  4. #4
    Join Date
    Feb 2017
    Posts
    344

    Re: What’s the difference between equals() and ==?

    Quote Originally Posted by arshi1586 View Post
    What’s the difference between equals() and ==?
    The test r1==r2 checks whether the two references r1 and r2 hold the exact same object or not.

    On the other hand r1.equals(r2) checks whether the objects held by r1 and r2 have the same content. What's to be considered "the same content" is up to the programmer to determine by overriding the equals() method defined in the Object class. The Object class is the Java top class and it's always automatically inherited by every other class so equals() is available everywhere to use as it is or to override.

    If r1==r2 is true then r1.equals(r2) must be true because the same object obviously has the same content. If r1==r2 is false then r1.equals(r2) might still be true because different objects can have the same content indeed.

    If the programmer defines a class but doesn't override equals() and then still compares two objects of that class using r1.equals(r2) then the outcome will be the same as if r1==r2 was used. It's because that's how equals() is implemented in the Object class.

    To learn how to go about to override equals() it is best to look at an example in a tutorial.
    Last edited by wolle; June 20th, 2018 at 01:59 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)