Fundamentals 4 min read

Can a Variable Be Both 1 and 12? Exploring Impossible JavaScript & Java Tricks

This article examines the puzzling condition if(a==1 && a==12) and demonstrates how JavaScript and Java can be coaxed to satisfy it using clever language tricks such as property getters, reflection, and cache manipulation, revealing deeper insights into language internals.

Programmer DD
Programmer DD
Programmer DD
Can a Variable Be Both 1 and 12? Exploring Impossible JavaScript & Java Tricks

Introduction

While browsing a programming community, a seemingly impossible JavaScript snippet was posted: a variable that simultaneously satisfies if(a == 1 && a == 12). The author initially thought it was nonsense, but the problem turned out to be an intriguing exploration of language quirks.

var a = ???;
if(a == 1 && a == 12){
  console.log(a);
}

The challenge invites readers to think beyond ordinary variable semantics and consider how a value could change dynamically during evaluation.

JavaScript Solution

One common approach in JavaScript is to use a getter or a Proxy that returns different values on each access, making the condition appear true. By redefining how the variable is read, the same identifier can yield 1 on the first check and 12 on the second.

Java Solution

Java offers a more low‑level trick by tampering with the internal Integer cache via reflection. The following code demonstrates the technique:

Class cache = Integer.class.getDeclaredClasses()[0];
Field c = cache.getDeclaredField("cache");
c.setAccessible(true);
Integer[] array = (Integer[]) c.get(cache);
// array[129] is 1
array[130] = array[129]; // Set 2 to be 1
array[131] = array[129]; // Set 3 to be 1
Integer a = 1;
if(a == (Integer)1 && a == (Integer)2 && a == (Integer)3){
   System.out.println("Success");
}

An alternative answer employs PowerMockRunner to mock the Integer cache, achieving the same effect with a testing framework.

Conclusion

The article is not meant to exhaustively catalog language features but to showcase a fun puzzle that tests both knowledge of language internals and problem‑solving methodology. It highlights how creative use of reflection, proxies, or mocking can turn an impossible condition into a demonstrable truth.

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.

JavaJavaScriptReflectionProgramming PuzzleCode Trick
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.