someguy567 Posted November 7, 2018 Posted November 7, 2018 (edited) I'm wondering in this example, which would be more appropriate? getGrandExchange().getStatus(GrandExchange.Box.BOX_1) == GrandExchange.Status.FINISHED_SALE or getGrandExchange().getStatus(GrandExchange.Box.BOX_1).equals(GrandExchange.Status.FINISHED_SALE) Edited November 7, 2018 by someguy567
Eagle Scripts Posted November 7, 2018 Posted November 7, 2018 (edited) 20 hours ago, someguy567 said: I'm wondering in this example, which would be more appropriate? getGrandExchange().getStatus(GrandExchange.Box.BOX_1) == GrandExchange.Status.FINISHED_SALE or getGrandExchange().getStatus(GrandExchange.Box.BOX_1).equals(GrandExchange.Status.FINISHED_SALE) One should make it a practice to always use #equals. Even when one is in a situation where references have to be compared (==) for a custom Class, one should still override the #equals method for it to implement reference comparing. ^ This does not apply to primitives and constants though . Edited November 8, 2018 by Eagle Scripts 1
Ragnar Lothbrok Posted November 7, 2018 Posted November 7, 2018 https://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java
FrostBug Posted November 8, 2018 Posted November 8, 2018 (edited) Use == for primitives and constants Use .equals() for everything else GrandExchange.Status is an enum, and enum values are always constant (even if they for some reason are mutable), so here you should use '==' 15 hours ago, Alek said: The following is not valid because String is an object: String s = "abc"; if(s == "abc") { } Those strings are literals actually; which are constant. '==' is valid Edited November 8, 2018 by FrostBug 1
dreameo Posted November 9, 2018 Posted November 9, 2018 16 hours ago, FrostBug said: Use == for primitives and constants Use .equals() for everything else GrandExchange.Status is an enum, and enum values are always constant (even if they for some reason are mutable), so here you should use '==' Those strings are literals actually; which are constant. '==' is valid I think the main reason why what Alek wrote is valid is because both those string objects point to the same object. Compiler is smart enough to see that "abc" is being used twice and instead of making 2 separate objects, it makes both variables point to one "abc" object. (90%)
FrostBug Posted November 9, 2018 Posted November 9, 2018 8 hours ago, dreameo said: I think the main reason why what Alek wrote is valid is because both those string objects point to the same object. Compiler is smart enough to see that "abc" is being used twice and instead of making 2 separate objects, it makes both variables point to one "abc" object. (90%) Yes, constants as in the same object. Java uses whats called a String constant pool where all string literals are stored. When creating the second literal, the pool is checked, and a reference to the previously added literal is returned 1 1
Chris Posted November 9, 2018 Posted November 9, 2018 On 11/8/2018 at 3:50 AM, FrostBug said: Use == for primitives and constants Use .equals() for everything else GrandExchange.Status is an enum, and enum values are always constant (even if they for some reason are mutable), so here you should use '==' Those strings are literals actually; which are constant. '==' is valid fake news!!! nice try CNN! 1