Solved Same objects not equal.

Discussion in 'Plugin Development' started by sebcio98, Sep 8, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    sebcio98

    Here's a test code I've made to resolve the problem:
    Code:
    ItemStack item1 = new ItemStack(Material.WOOD, 2, (byte)1);
    ItemStack item3 = new ItemStack(Material.WOOD, 2, (byte)1);//same as item1
    SerItemStack sitem1 = new SerItemStack(item1);//custom class, code below
    SerItemStack sitem2 = new SerItemStack(item1);
    
    System.out.println(item1.equals(item3));//true
    System.out.println(sitem1.equals(sitem2));//false
    And the SerItemStack.class:
    Code:
    *code removed, because unnecessary*
    My question is simple. Why? Why are the same (for me) instances of my class not equal?
     
    Last edited: Sep 8, 2016
  2. Offline

    timtower Administrator Administrator Moderator

    @sebcio98 Not the exact same things as it points to different locations (complicated stuff)
    Try overriding equals instead
     
  3. Offline

    I Al Istannen

    @sebcio98
    The default equals method uses "==" internally. This is defined in the "Object" class, which every class implicitly extends. It provides the hashCode, equals methods and a few more things.

    The "==" operator compares where the objects lie in memory. Look at this:

    memory_location.png


    Here, 0 and 6 are logically equal (both "Hello") but lie at different locations. Hence "==" returns false.

    So, the programmer should overwrite equals, to provide a logical comparison, not a memory address one.
    Which means, do what @timtower said ;)

    "==" works for primitve values (not really well for float and double, but that is another story) and enums.
    Why enums?
    Well, every objects is essentially a pointer to the memory address it lies in. Two or more objects can point at the same memory adress.
    For enums, there is only one instance, which all objects point to. Therefore the comparison of the memory address is successful (and often preferred, as it is null and type safe).

    Of course this was a bit simplified (and maybe not totally correct, but the way I learned it and that makes sense), so ask if you have any more questions!
     
    Zombie_Striker and bwfcwalshy like this.
  4. Offline

    sebcio98

    @timtower Oh, didn't know the default equals method compares addresses. Well, it's not really complicated stuff, I'm not scared of memory pointers :)

    EDIT: I just read your post @I Al Istannen.
    Wow. I am impressed by your explanation. That's a really great post. Can't say I learned something though. I have programmed in c++ for more than 5 years now. But I hope you'll keep up the quality :)
     
    Last edited: Sep 8, 2016
  5. Offline

    I Al Istannen

    @sebcio98
    Thank you really much! :)
    I am normally not the one you would ask for an explanation, I can't seem to deliver the concept most of the time :p

    Well, I didn't know your c++ background (and I think it is a great thing you spice languages up a bit. Got to keep that brain busy ;)), but that makes me just happier :)

    I do hope that too :p Sometimes I get the motivation to explain a concept I am familiar with (or just learned. The joy :p) and hope I will succeed in doing so.

    Have a great day!

    (And as I hope your question was answere, consider marking this as solved :)
    Top right of the Thread, "Thread Tools" > "Edit title" > And change the prefix. Just throwing that out, as it is hidden quite well :p)
     
Thread Status:
Not open for further replies.

Share This Page