Zum Hauptinhalt springen
🕵️ SicherheitslückenCVE-2026-20716 | Intel Processors access control (Nessus ID 346889)(18.09.2026 um 02:41 Uhr)
🕵️ SicherheitslückenCVE-2026-20713 | Intel Xeon processors control flow (Nessus ID 346889)(18.09.2026 um 02:41 Uhr)
🕵️ SicherheitslückenCVE-2026-20716 | Intel Processors access control (Nessus ID 346889)(18.09.2026 um 02:41 Uhr)
🕵️ SicherheitslückenCVE-2026-20713 | Intel Xeon processors control flow (Nessus ID 346889)(18.09.2026 um 02:41 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Understanding == in Java: Why 1 == 1 is True but 128 == 128 Might Be False

If you’re learning Java, you’ve likely used the== operator countless times. It’s straightforward when dealing with numbers, right? Well, not always. A common point of confusion arises when 1 == 1evaluates to true, but surprisingly, 128 == 128 might evaluate to false. What’s going on? Is Java broken? Absolutely not! The key lies in understanding primitive types, object references, and autoboxing in Java. Let’s unravel this mystery step by step.

Image description

The Basics: Primitive vs Object Comparison
In Java, the == operator behaves differently depending on whether you are comparing primitives or objects:

For primitives(e.g., int, char, double):
The == operator compares the actual values. If the values are the same, the result is true.

int x = 1;
int y = 1;
System.out.println(x == y); // true

For objects (e.g., Integer, String, or custom classes):
The == operator checks whether the two objects point to the same memory location (i.e., reference equality).

Integer x = new Integer(128);
Integer y = new Integer(128);
System.out.println(x == y); // false (different memory locations)

What About Autoboxing?
Java introduced autoboxing in Java 5 to make working with primitives and their wrapper classes (like int and Integer) more seamless. It allows you to write code like this.

Integer a = 1; // Autoboxes the int 1 to an Integer object
int b = a;     // Unboxes the Integer back to a primitive int

While this is convenient, it can lead to subtle bugs when combined with ==. Why? Because objects and primitives behave differently with ==.

The Integer Cache
Java optimizes memory usage by caching Integer objects for values between -128 and 127. This means if you create two Integer objects within this range, they will point to the same memory location. Let’s see this in action.

Integer x = 127;
Integer y = 127;
System.out.println(x == y); // true (same cached object)

Integer a = 128;
Integer b = 128;
System.out.println(a == b); // false (different objects)

When comparing x and y (both 127), Java reuses the same cached object. For a and b (both 128), Java creates two distinct Integer objects outside the cached range. Hence, a == b evaluates to false.

Why Does This Happen?
The caching behavior is defined in the Integer class.

private static class IntegerCache {
   static final Integer[] cache = new Integer[-(-128) + 127 + 1];
   ...
}

By default, Java caches Integer values in the range -128 to 127 to save memory. For numbers outside this range, new objects are created.

How to Avoid This Confusion
If you want to compare the values of two Integer objects (or other wrapper classes), you should use the .equals() method, or explicitly unbox them into primitives.

Using .equals()

Integer a = 128;
Integer b = 128;
System.out.println(a.equals(b)); // true

Unboxing to Primitives

Integer a = 128;
Integer b = 128;
System.out.println(a.intValue() == b.intValue()); // true

Working with Primitives Directly

int a = 128;
int b = 128;
System.out.println(a == b); // true

Key Takeaways

Primitive Comparison: == compares the actual values.

int x = 1, y = 1;
System.out.println(x == y); // true

Object Comparison: == compares memory references, not values.

Integer x = 128, y = 128;
System.out.println(x == y); // false

Integer Cache: Java caches Integer objects for values between -128 and 127.

Integer x

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Understanding == in Java: Why 1 == 1 is True but 128 == 128 Might Be False

Thematisch verwandte Begriffe: Understanding, Java, True, Might · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-61591 | djust provides Phoenix LiveView-style reactive server-side rendering for…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
News ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

↗ Original-Quelle