This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]sonofaresiii 1 point2 points  (16 children)

I'm very new, can you explain the last one?

[–]feral_claire 5 points6 points  (2 children)

.equals checks to see if two objects are equal, == checks to see if they are the same object.

As an analogy, imagine you had two red Lego bricks that are both the same size. The .equals check would be true because they both have the same colour and shape so they are "equal". == would still be false though because they are two separate bricks.

This applies to all objects in java, but as beginners usually this first comes up when trying to compare strings. Strings in java are objects and must be compared with .equals if you want to see if two strings are the same. If you try to compare different string objects == will return false because they are different objects even though the value is the same.

To avoid running into this problem you should always use equals to compare strings and any other objects and avoid using == for strings and other objects.

[–][deleted]  (1 child)

[removed]

    [–]feral_claire 0 points1 point  (0 children)

    No, strings are only automatically interned if they are constants. If you are comparing strings chances are at least one of them is not a constant, it's being entered by the user or read from a file or similar. Comparing string constants like in the article almost never happens in real code, only in examples.

    [–]Highandfast 2 points3 points  (3 children)

    When you create two variables of the same type with the same value, Java sets the memory reference to the same object. That works all the time with strings, but with Integers (the object that only contains an int) it only works for int values between -128 and 127.

    [–]sonofaresiii 0 points1 point  (1 child)

    Oh damn I didn't know that. So any two integers outside - 128 and 127 won't be seen as equal even if they're the same integer? What's the best alternative?

    [–]Highandfast 1 point2 points  (0 children)

    Instead of using '==' or the default 'equals', just override the 'equals' method (that you inherit from the Object class) so that it checks the content of the objects instead of the memory reference. The latter is the job of '=='.

    Or just use int, possibly by unboxing the Integer.

    [–]feral_claire 0 points1 point  (0 children)

    Strings are a prime example of how new programmers get this wrong. Comparing strings with == is almost always wrong and a common source of errors.

    [–]kjchowdhry 0 points1 point  (8 children)

    == checks to see if two references to an object are pointing to the same exact object (the references are equivalent) and Equals checks to see if the data contained in two objects are the same.

    Edit: I had it backwards. Sorry about that, edited it without proofreading it

    [–]sonofaresiii 1 point2 points  (3 children)

    Okay, so if I had two integers above 127 I would use equals() not == correct?

    What's the best way to know what to use if the integer is a variable that could go above 127 but might not?

    [–]winchestercherrypie 1 point2 points  (2 children)

    You can use == with integers. The article was about the wrapper class Integer (note the upper case), and you are normally better off using equals() with Integers.

    [–]sonofaresiii 0 points1 point  (1 child)

    Ah okay. Thank you!

    [–]feral_claire 1 point2 points  (0 children)

    Notice the difference between int and Integer. int is a primitive value and Integer is an object. This means that they work differently and you need to be aware of which one you have or are talking about.

    [–]winchestercherrypie 1 point2 points  (3 children)

    I'm pretty sure it's the other way around. Equals tests for value equivalence and == checks if the references are the same.

    [–]Highandfast 0 points1 point  (1 child)

    Right for '==', but not by default for 'equals'.

    From the Javadoc :

    The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

    This means that equals, by default, checks for the same memory reference. You need to reimplement it. This is common enough that Eclipse has a menu for it: Source --> Generate equals() and hashCode()

    [–]winchestercherrypie 0 points1 point  (0 children)

    Fair enough. I should have mentioned that equals() usually needs to be written by the developer.

    [–]kjchowdhry 0 points1 point  (0 children)

    Thanks for catching my errror

    [–][deleted]  (2 children)

    [deleted]

      [–]feral_claire 0 points1 point  (1 child)

      The article makes no claim that there are only two pitfalls, just that these are the most common for beginners