Solved How is this Executing?

Discussion in 'Plugin Development' started by AngryCupcake274, Nov 12, 2015.

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

    AngryCupcake274

    Hello,
    I have the following code:
    Code:
                    LivingEntity ent = null;
                    p.sendMessage("'" + variant + "'");
                    if (variant == "HORSE" || variant == "DONKEY" || variant == "MULE") {
                       
                        p.sendMessage("y");
                        ent = (Horse) p.getLocation().getWorld().spawnCreature(loc, EntityType.fromName(entName));
                       
                    } else {
                       
                        p.sendMessage("n");
                        ent = p.getLocation().getWorld().spawnCreature(loc, EntityType.fromName(entName));
                       
                    }
    variant = "HORSE" exactly (no spaces, etc.)

    Does anyone know why I'm getting the message "n" instead of "y"?
     
  2. Offline

    Xerox262

    Are you trying to compare Strings with ==?
     
  3. Offline

    AngryCupcake274

    Yes. Should I be using .equals()?
     
  4. Offline

    tkuiyeager1

    Yes.

    @AngryCupcake274
    I don't know if it's matter but maybe you should rename the LivingEntity To EntityType.
     
  5. Offline

    AngryCupcake274

    Why does that matter? You'd think == would work too...
     
  6. Offline

    CraftCreeper6

    @AngryCupcake274
    Is variant a string? If so, you should be using .equals().
     
  7. Offline

    AngryCupcake274

    Why does that matter though?
     
  8. Offline

    timtower Moderator Moderator

    Well, == won't work for strings, where equals does
     
  9. Offline

    Xerox262

    • "== is a reference comparison, i.e. both objects point to the same memory location
    • .equals() evaluates to the comparison of values in the objects"
    http://stackoverflow.com/questions/7520432/java-vs-equals-confusion

    This is basic Java, you should know Java before delving into the Bukkit API
     
  10. Offline

    AngryCupcake274

  11. Offline

    timtower Moderator Moderator

  12. Offline

    AngryCupcake274

    Doesn't that make sure the variable types are the same as well?
     
  13. Offline

    timtower Moderator Moderator

    It does more than that, and you don't want to do what that method can.
     
  14. Offline

    AngryCupcake274

    So I'll just use '.equals()' instead of '==' or '==='. Thank you guys!

    I'm gonna have to look into '==' and '===' more. My life is a lie! :p
     
  15. Offline

    timtower Moderator Moderator

    Mrs. bwfctower likes this.
  16. Offline

    AngryCupcake274

    Ok. :)
     
  17. Offline

    rbrick

    @AngryCupcake274
    Considering there is no '===' operator in Java, good luck with that.

    As mentioned above '==' is for reference equality (Does this object point to the same address in memory). What you don't see happening in Java is the memory allocations, in languages such as C/C++ you have more access to things like this, so its more apparent. When you create a String, lets say "Hello World", this creates what is known as an address, which points to a value in memory.

    So lets say you have the address 0x00000001 (Addresses are commonly represented in Hex) and this points to our String "Hello World" (0x00000001 -> "Hello World"). Now lets do the == comparison which, as aforementioned, this will compare if two objects have the same address so let us say we have another string "Hello World." (Notice the period), because it is not the same string as above (Note: Java will tend to do some optimization meaning that "Hello World" == "Hello World", would work, because it will just reuse the same address, Do not take this as a "content" equality test, they simply point to the same object in memory), it will have allocated a new address, meaning that the string addresses do not match. "equals()" (or equalsIgnoreCase() is another option with strings), will test (if implemented/overridden) "content" equality (Object equality, i.e. do the objects have the same values), meaning that variant.equals("HORSE"); will always be true if the variant is equal to "HORSE"
     
  18. Offline

    AngryCupcake274

    I bow down to you.
     
Thread Status:
Not open for further replies.

Share This Page