Jump to content

== vs equals


someguy567

Recommended Posts

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 by Eagle Scripts
  • Like 1
Link to comment
Share on other sites

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 :xboge:

Edited by FrostBug
  • Like 1
Link to comment
Share on other sites

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 :xboge:

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%)

Link to comment
Share on other sites

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

  • Like 1
  • Boge 1
Link to comment
Share on other sites

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 :xboge:

fake news!!! nice try CNN!

  • Sad 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...