Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. How to compare two classes instances

How to compare two classes instances

Scheduled Pinned Locked Moved Java
tutorialquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    khurram_shahzad
    wrote on last edited by
    #1

    How i campare two objects, such that if i have class(given below) and i want to compare there two different instances then how ??

    class MyNode {
    MyNode left;
    MyNode right;
    }

    Is there any possiblity to do that --->

    public boolean MyFunction(MyNode toSearch, MyNode parentNode) {
    MyNode currentNode = parentNode;
    while(currentNode != toSearch) {
    return true;
    }
    else {
    currentNode = currentNode.left;
    }
    }
    return false;

    L C 2 Replies Last reply
    0
    • K khurram_shahzad

      How i campare two objects, such that if i have class(given below) and i want to compare there two different instances then how ??

      class MyNode {
      MyNode left;
      MyNode right;
      }

      Is there any possiblity to do that --->

      public boolean MyFunction(MyNode toSearch, MyNode parentNode) {
      MyNode currentNode = parentNode;
      while(currentNode != toSearch) {
      return true;
      }
      else {
      currentNode = currentNode.left;
      }
      }
      return false;

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You should implement your own Equals() method in your class and use that to test for equality. Using the == and != operators will not give the result that you are looking for.

      I must get a clever new signature for 2011.

      K C 2 Replies Last reply
      0
      • L Lost User

        You should implement your own Equals() method in your class and use that to test for equality. Using the == and != operators will not give the result that you are looking for.

        I must get a clever new signature for 2011.

        K Offline
        K Offline
        khurram_shahzad
        wrote on last edited by
        #3

        but can you please give some guide line to do this ... such that one way in my mind is like this that i put an Interger property in class that are then use to identify different instanceof class .. such that 1 for 1st instance/node then 2 for 2nd and so on and then compare some thing like this NodeA.IntegerIdentifier == NodeB.IntegerIdentifier but i doesn't satisfy with this idea as it is not apply in real senario as because in programming we should try to make solution in some thing like general awy. So is there any idea aur suggestion for me that how i achieve this equality problem/isue what ever :)

        L 1 Reply Last reply
        0
        • K khurram_shahzad

          but can you please give some guide line to do this ... such that one way in my mind is like this that i put an Interger property in class that are then use to identify different instanceof class .. such that 1 for 1st instance/node then 2 for 2nd and so on and then compare some thing like this NodeA.IntegerIdentifier == NodeB.IntegerIdentifier but i doesn't satisfy with this idea as it is not apply in real senario as because in programming we should try to make solution in some thing like general awy. So is there any idea aur suggestion for me that how i achieve this equality problem/isue what ever :)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          See here[^] for lots of information on object equality.

          I must get a clever new signature for 2011.

          1 Reply Last reply
          0
          • K khurram_shahzad

            How i campare two objects, such that if i have class(given below) and i want to compare there two different instances then how ??

            class MyNode {
            MyNode left;
            MyNode right;
            }

            Is there any possiblity to do that --->

            public boolean MyFunction(MyNode toSearch, MyNode parentNode) {
            MyNode currentNode = parentNode;
            while(currentNode != toSearch) {
            return true;
            }
            else {
            currentNode = currentNode.left;
            }
            }
            return false;

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            khurram_shahzad wrote:

            and i want to compare there two different instances then how ??

            Your question is not really clear. There's a difference between having the same instance and having two different instances which are the same. In the first case, you want to verify if you are talking about the same instance. That's what you do in the code sample you posted: you check wether currentNode is the same object as toSearch. In the second case, you want to verify that two instances contain the same thing but they are most of the time two different objects. In your case, from what I saw in your code sample, you want to verify whether you are talking about the same object. Thus, it is fine to write it the way you did and it will work fine (in fact, when you use the == operator with objects, it will compare if the adresses of the two objects are the same, thus return true if the two pointers point to the same class instance).

            Cédric Moonen Software developer
            Charting control [v3.0] OpenGL game tutorial in C++

            K 1 Reply Last reply
            0
            • L Lost User

              You should implement your own Equals() method in your class and use that to test for equality. Using the == and != operators will not give the result that you are looking for.

              I must get a clever new signature for 2011.

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              I think the OP wanted to search for a specific class instance in a linked list (at least, that's what I guessed from his code snippet). Thus, his code using the == operator was fine.

              Cédric Moonen Software developer
              Charting control [v3.0] OpenGL game tutorial in C++

              L K 2 Replies Last reply
              0
              • C Cedric Moonen

                I think the OP wanted to search for a specific class instance in a linked list (at least, that's what I guessed from his code snippet). Thus, his code using the == operator was fine.

                Cédric Moonen Software developer
                Charting control [v3.0] OpenGL game tutorial in C++

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                you are right Cédric, I did not spend enough time reading the question.

                I must get a clever new signature for 2011.

                1 Reply Last reply
                0
                • C Cedric Moonen

                  I think the OP wanted to search for a specific class instance in a linked list (at least, that's what I guessed from his code snippet). Thus, his code using the == operator was fine.

                  Cédric Moonen Software developer
                  Charting control [v3.0] OpenGL game tutorial in C++

                  K Offline
                  K Offline
                  khurram_shahzad
                  wrote on last edited by
                  #8

                  Thanks A LLLot .... Thanks for your great help ....

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    khurram_shahzad wrote:

                    and i want to compare there two different instances then how ??

                    Your question is not really clear. There's a difference between having the same instance and having two different instances which are the same. In the first case, you want to verify if you are talking about the same instance. That's what you do in the code sample you posted: you check wether currentNode is the same object as toSearch. In the second case, you want to verify that two instances contain the same thing but they are most of the time two different objects. In your case, from what I saw in your code sample, you want to verify whether you are talking about the same object. Thus, it is fine to write it the way you did and it will work fine (in fact, when you use the == operator with objects, it will compare if the adresses of the two objects are the same, thus return true if the two pointers point to the same class instance).

                    Cédric Moonen Software developer
                    Charting control [v3.0] OpenGL game tutorial in C++

                    K Offline
                    K Offline
                    khurram_shahzad
                    wrote on last edited by
                    #9

                    Yes here im comparing to diffrent instace of same class ..... Thanks A LLLot .... Thanks for your great help .... :-D

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups