Lädt...


🔧 Mind-blowing: Why 1 == 1 is 🟢 true, but 128 == 128 is 🔴 false in Java?


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

In Java, the == operator checks for reference equality, meaning it compares whether the two variables point to the same object in memory.

The .equals() method checks for value equality, meaning it compares the actual values of the objects.

Integer Caching in Java

Java caches Integer objects for values in the range of -128 to 127 for performance reasons. When you use Integer objects (not int primitives) in this range, Java reuses the same object references, so comparisons using == will return true.

Integer a = 1;
Integer b = 1;
System.out.println(a == b);  // true, because both reference the same cached object

However, outside the cached range (e.g., for 128 and beyond), new Integer objects are created, so the references are different.

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

Correct Comparison with .equals()

To compare the actual values of Integer objects, you should use .equals() instead of ==:

Integer a = 128;
Integer b = 128;
System.out.println(a.equals(b));  // true, because it compares values, not references

In summary:

  • 1 == 1 works because both objects point to the same cached reference.
  • 128 == 128 returns false because Java creates separate objects for values outside the caching range.

If you’re comparing values and not references, always use .equals() for non-primitive types like Integer.

...

🔧 CodeSOD: False True is True False


📈 46.7 Punkte
🔧 Programmierung

🔧 Why is 1 == 1 is true but 1000 == 1000 is false When dealing with Wrapper Classes in Java?


📈 39.69 Punkte
🔧 Programmierung

🔧 Why does 1 < 2 < 3 return true but 3 > 2 > 1 return false in JavaScript?


📈 34.02 Punkte
🔧 Programmierung

📰 Antivirus-Fehlalarm: Was tun? False Positive vs. False Negative - COMPUTER BILD


📈 26.61 Punkte
📰 IT Security Nachrichten

🔧 Visualizing Truth: Large Language Models Linearly Separate True and False Statements


📈 23.35 Punkte
🔧 Programmierung

🔧 Boolean: True and False


📈 23.35 Punkte
🔧 Programmierung

📰 Python Set Is Way Faster Than List, True Or False?


📈 23.35 Punkte
🔧 AI Nachrichten

📰 True Protection or False Promise? The Ultimate ITDR Shortlisting Guide


📈 23.35 Punkte
📰 IT Security Nachrichten

🔧 Creating a True/False Toggle in React with useState Hook for Beginners


📈 23.35 Punkte
🔧 Programmierung

🪟 Microsoft: People aren't 'interested' in tech corps telling them what's true or false


📈 23.35 Punkte
🪟 Windows Tipps

📰 True or false? How to spot – and stop – fake news


📈 23.35 Punkte
📰 IT Security Nachrichten

🐧 Is there a way to check if you have internet connection and if true run command X if false break? Centos


📈 23.35 Punkte
🐧 Linux Tipps

📰 True/False? “NHTSA reports an average of one accident per 484,000 miles”


📈 23.35 Punkte
📰 IT Security Nachrichten

📰 When Is a “Literally True” Statement False and a Crime


📈 23.35 Punkte
📰 IT Security Nachrichten

📰 When Is a “Literally True” Statement False and a Crime


📈 23.35 Punkte
📰 IT Security Nachrichten

📰 False Positives Are a True Negative: Using Machine Learning to Improve Accuracy


📈 23.35 Punkte
📰 IT Security Nachrichten

📰 This Weird Trick Tells You If Any News Story Is True Or False!


📈 23.35 Punkte
📰 IT Security Nachrichten

📰 Google Goes Global With "False" or "True" Labels on Search Results


📈 23.35 Punkte
📰 IT Security Nachrichten

🔧 Exploring the Core of true value vs false value in JavaScript...


📈 23.35 Punkte
🔧 Programmierung

🔧 Dive into the Complexity of the Human Mind with "The Society of Mind"


📈 22.73 Punkte
🔧 Programmierung

🔧 Harnessing the Power of Digital Mind Mapping: A Guide for the Productive Mind


📈 22.73 Punkte
🔧 Programmierung

🍏 How to make a mind map: The best mind mapping techniques and tools


📈 22.73 Punkte
🍏 iOS / Mac OS

🪟 Superhot: Mind Control Delete adds mind-bending roguelike twists


📈 22.73 Punkte
🪟 Windows Tipps

🐧 Basic directory description for refresh your mind, keep it on your mind :-)


📈 22.73 Punkte
🐧 Linux Tipps

🔧 Java, but why? The state of Java in 2024


📈 22.01 Punkte
🔧 Programmierung

⚠️ Kaspersky & Probrand Group: True partnership delivers True Cybersecurity


📈 20.1 Punkte
⚠️ Malware / Trojaner / Viren

📰 TaoTronics SoundLiberty 53 True Wireless Earbuds Review - True AirPods Pro Rival


📈 20.1 Punkte
📰 IT Security Nachrichten

📰 iOS 11 GM Leak verrät iPhone 8 Features: True Tone Display, True Tone Display ...


📈 20.1 Punkte
📰 IT Nachrichten

📰 Leistungsschutzrecht: Memes und Vorschaubilder sollen zukünftig nur noch 128 x 128 Pixel groß sein


📈 19.79 Punkte
📰 IT Nachrichten

📰 Leistungsschutzrecht: Memes sollen nur noch 128 mal 128 Pixel groß sein


📈 19.79 Punkte
📰 IT Nachrichten

matomo