Natix
Natix

Reputation: 14247

Can Java's == operator be asymmetric for primitive values?

Can Java's == operator be asymmetric for primitive values, so that x == y, but y != x, where x and y are both variables of some (possibly different) primitive types?

Edit:

OK, I'll be honest with the reason why I ask. In the Java Puzzlers book, there is this puzzle #87 which concerns the == operator for primitive types.

It has three parts, one challenges reader to find a case where the == operator is not reflective, second is for finding a case where == is not transitive. I've found solutions for both of them, but I have no idea how to solve the third one, which is defined like this:

public class Symmetric {
    public static void main(String[] args) throws Exception {
        /*
         * If you can come up with a set of primitive types and values
         * that causes this program to print "true false", then
         * you have proven that the == operator is not symmetric.
         */
        <typeX> x = <valueX>;
        <typeY> y = <valueY>;

        System.out.print ((x == y) + " ");
        System.out.println(y == x);
    }
}

I don't own the book, so I cannot look it up for a solution, I only got across the source files, which don't contain the solutions.

Upvotes: 0

Views: 288

Answers (4)

Peter Lawrey
Peter Lawrey

Reputation: 533502

There is a only are a few special values for equality and comparison. These are Float.NaN, Double.NaN, -0.0f and -0.0.

The reason these are special is that they don't follow the some of the normal rules for equality or compareTo

public static void main(String... args) {
    printComparisons("Float.NaN", Float.NaN);
    printComparisons("Double.NaN", Double.NaN);
    printComparisons("-0.0f", -0.0f);
    printComparisons("-0.0", -0.0);
}

private static void printComparisons(String desc, float v) {
    System.out.println("[ " + desc + " ]");
    System.out.println(v + " == " + v + " is " + (v == v));
    System.out.println(v + " != " + v + " is " + (v != v));
    System.out.println(v + " == 0.0  is " + (v == 0.0));
    System.out.println(v + " < 0 is " + (v < 0));
    System.out.println(v + " > 0 is " + (v > 0));
    System.out.println("Float.compareTo(" + v + ", 0) is " + Float.compare(v, 0));
    System.out.println();
}

private static void printComparisons(String desc, double v) {
    System.out.println("[ " + desc + " ]");
    System.out.println(v + " == " + v + " is " + (v == v));
    System.out.println(v + " != " + v + " is " + (v != v));
    System.out.println(v + " == 0.0  is " + (v == 0.0));
    System.out.println(v + " < 0 is " + (v < 0));
    System.out.println(v + " > 0 is " + (v > 0));
    System.out.println("Double.compareTo(" + v + ", 0) is " + Double.compare(v, 0));
    System.out.println();
}

prints

[ Float.NaN ]
NaN == NaN is false
NaN != NaN is true
NaN == 0.0  is false
NaN < 0 is false
NaN > 0 is false
Float.compareTo(NaN, 0) is 1

[ Double.NaN ]
NaN == NaN is false
NaN != NaN is true
NaN == 0.0  is false
NaN < 0 is false
NaN > 0 is false
Double.compareTo(NaN, 0) is 1

[ -0.0f ]
-0.0 == -0.0 is true
-0.0 != -0.0 is false
-0.0 == 0.0  is true
-0.0 < 0 is false
-0.0 > 0 is false
Float.compareTo(-0.0, 0) is -1

[ -0.0 ]
-0.0 == -0.0 is true
-0.0 != -0.0 is false
-0.0 == 0.0  is true
-0.0 < 0 is false
-0.0 > 0 is false
Double.compareTo(-0.0, 0) is -1

In summary.

  • NaN is not >, <, ==, >= or <= 0.0 but for compareTo(NaN,0) return 1
  • 0.0 is == 0.0 but for compareTo(-0.0, 0.0) return -1

Upvotes: 4

Oleksi
Oleksi

Reputation: 13097

No. For all primitives, the == sign is commutative. That is,

(x == y) => (y == x)

Upvotes: 4

NominSim
NominSim

Reputation: 8511

== Is symmetric for every type...

From the spec:

The equality operators are commutative if the operand expressions have no side effects.

Upvotes: 4

Steven Schlansker
Steven Schlansker

Reputation: 38526

I'm not aware of any cases for which this is true for primitive types.

Upvotes: 2

Related Questions