Fundamentals 6 min read

How Can an Object Equal Two Different Values in JavaScript and Java?

This article explains how to make an object appear equal to two distinct values by exploiting the valueOf method in JavaScript and a custom equalsTo method in Java, showing step‑by‑step code examples and the underlying state‑changing logic.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
How Can an Object Equal Two Different Values in JavaScript and Java?

JavaScript Implementation

The trick starts with an object that overrides valueOf to return a different number each time it is called. The object also holds a mutable property i that is incremented on the first comparison.

var obj = {
  i: 10,
  valueOf: function() {
    if (this.i === 10) {
      this.i++;
      return 10;
    } else {
      return 100;
    }
  }
};
if (obj == 10 && obj == 100) {
  console.log(obj);
}

When the condition obj == 10 && obj == 100 is evaluated, JavaScript performs a non‑strict comparison, which calls obj.valueOf(). The first call returns 10, satisfying the first part of the condition, and increments i to 11. The second call now returns 100, satisfying the second part, so the whole expression evaluates to true and the object is logged:

{ i: 11, valueOf: [Function: valueOf] }

A more compact version uses a self‑incrementing valueOf:

let obj = {
  i: 1,
  valueOf() { return this.i++; }
};
if (obj == 1 && obj == 2 && obj == 3) {
  console.log("Trick succeeded!");
}

Output:

Trick succeeded!

Java Implementation

Java cannot overload the == operator, so the same effect is achieved with a custom method that checks the current state and returns a different result each time.

public class Test {
    private int i = 10;
    public boolean equalsTo(int value) {
        if (i == 10) {
            i++;
            return value == 10;
        } else {
            return value == 100;
        }
    }
    @Override
    public String toString() {
        return "MagicObject{i=" + i + "}";
    }
    public static void main(String[] args) {
        Test obj = new Test();
        if (obj.equalsTo(10) && obj.equalsTo(100)) {
            System.out.println(obj);
        } else {
            System.out.println("Condition not met");
        }
    }
}

Running the program prints:

MagicObject{i=11}

A similar, more concise version can use a mutable field and a single equalsTo method:

public class Test {
    private int val = 1;
    public boolean equalsTo(int input) {
        return input == val++;
    }
    public static void main(String[] args) {
        Test obj = new Test();
        if (obj.equalsTo(1) && obj.equalsTo(2) && obj.equalsTo(3)) {
            System.out.println("Trick succeeded!");
        }
    }
}

Output:

Trick succeeded!

Both implementations demonstrate that the core idea is a combination of a mutable internal state and a comparison method that returns different values on successive calls, turning an object into something that appears equal to multiple distinct values.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJavaScriptObject ComparisonvalueOfprogramming trickstateful method
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.