0

I'm comparing 2 Java objects using == operator, can someone explain me why the print "b" is and not "a"?

public class punktAusfuehren {

Punkt p1 = new Punkt(19, 10);
Punkt p2 = new Punkt(5, 0);

public static void main(String[] args) {
    new punktAusfuehren();
}

public punktAusfuehren() {
    if (p1 == p2) {
        System.out.println("a");
    } else {
        System.out.println("b");
    }

    if (p1 instanceof Punkt) {
        System.out.println("c");
    } else {
        System.out.println("d");
        }

   }

 }
3

4 Answers 4

5

Because they have different references. == gives reference equality if two object are referenced by the same reference or not, to compare two object use equals method instead. like

checks if two object are equal, but you have to override equals and hashCode method, to define equality

p1.equals(p2) //

An example Point class might look like this:

public class Point {

    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    // accessors omitted for brevity

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Point point = (Point) o;
        return x == point.x && y == point.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }
}

EDIT:

For object comparison, the equality ( == )operator is applied to the references to the objects, not the objects they point to. Two references are equal if and only if they point to the same object, or both point to null. See the examples below:

    Point x = new Point(3,4); 
    Point y = new Point (3,4); 
    Point z = x; 
    System.out.println(x == y); // Outputs false 
    System.out.println(x.equals(y) ); // Outputs true
   System.out.println(x == z); // Outputs true
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to recommend implementing it (and hashCode) as well. if he hasn't, and uses equals, he will use the inherited equals from Object, which is an == comparison.
2

In java the == operator will determine equivalence between Objects by comparing their memory locations and not the actual values. For example when comparing Strings, you would use stringA.equals(stringB) to compare the actual values.

To compare the object values, you will need to follow a similar approach and override the public boolean equals(Object o) method inside you Punkt class

Comments

1

In Java, if you use the '==' operator between two objects, it checks that both objects use the same address. Every time you call the 'new' operation, an object with a new address is created. In your code, p1 and p2 are each an object with a new address, so the '==' operator returns false.

Comments

0

== compares two object references not it's actual value, it checks if the two variables point to the same object or not.

If you want to compare objects, you need to use equals() method like p1.equals(p2).

1 Comment

I wouldn't consider a site that states you have to override equals(), while it is equals(Object o) and that claims that an equals becomes moderately hard to implement if there are/might be subclasses to be a very good source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.