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");
        }

   }

 }
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • 5
    Why do you expect `p1 == p2` to be true? – Eran Sep 14 '18 at 06:30
  • possible duplicate of this https://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator – Mostch Romi Sep 14 '18 at 06:31
  • 2
    Possible duplicate of [Compare two objects with .equals() and == operator](https://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator) – Ryuzaki L Sep 14 '18 at 06:32

4 Answers4

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
The Scientific Method
  • 2,374
  • 2
  • 14
  • 25
  • 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. – Stultuske Sep 14 '18 at 06:36
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

Stultuske
  • 9,296
  • 1
  • 25
  • 37
bradm6s
  • 102
  • 5
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.

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

imk
  • 374
  • 6
  • 12
  • 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 – Stultuske Sep 14 '18 at 06:55