How an Object Can Equal Two Different Values in JavaScript and Java
This article explains the clever trick of making a single object satisfy multiple equality checks in JavaScript using a mutable valueOf method, and shows how a similar effect can be achieved in Java through custom methods and state variables, highlighting the underlying mechanism.
The author discovered a bizarre trick where a single object can satisfy two different equality checks ( obj == 10 && obj == 100) in JavaScript, and demonstrates how to reproduce a comparable effect in Java.
JavaScript version
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);
}Output:
{ i: 11, valueOf: [Function: valueOf] }
The trick relies on the valueOf method returning a different value each time it is called. During the first comparison, obj.valueOf() returns 10, making obj == 10 true. The method also increments i to 11. On the second comparison, obj.valueOf() now returns 100, so obj == 100 is also true.
An even more concise 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 version
Java cannot overload the == operator, so the effect is achieved with a custom method that changes internal state.
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");
}
}
}Output:
MagicObject{i=11}
A more compact Java version uses a state variable that increments on each call:
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 rely on the same principle: a mutable internal state combined with comparison logic to make an object appear equal to multiple distinct values.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
