[Java] The problem that true was returned as a result of comparing Integer with ==

Conclusion

If ** == ** is used when comparing Integer (≠ int), there are ** combinations ** that return true for values ​​from -127 to +128.

What happened

The place where Integer was compared with ** == ** suddenly returned false from a certain point.

Occurs with a combination of the following two elements

1. 1. How to set a value to Integer

There are mainly the following three types. Integer i1 = 16; Integer i2 = Integer.valueOf(16); Integer i3 = new Integer(16)

The above pattern 1 is auto-unboxed, and the compiled result is a process using the valueOf method as in 2. (1 and 2 are equivalent) Reference: [Java] Auto Boxing, Unboxing

2. 2. Integer cache spec

Integer.valueOf () is a specification that returns the cache (sometimes the same instance) from -128 to +127 due to the specification in the normal setting. Reference: I read the source of Integer

What is the combination that returns true when comparing with ** == **?

The point that this matter is hard to notice

How to avoid the same problem as this case

I noticed if I set up a limit value test in the Unit test. I feel like there are many other things I can do.

Finally

It was a good notice because I didn't know the existence of the cache until this happened.

Recommended Posts

[Java] The problem that true was returned as a result of comparing Integer with ==
Let's express the result of analyzing Java bytecode with a class diagram
Be sure to compare the result of Java compareTo with 0
I made a program in Java that solves the traveling salesman problem with a genetic algorithm
A memorandum of the FizzBuzz problem
A story about hitting the League Of Legends API with JAVA
A story that struggled with the introduction of Web Apple Pay
Rubocop-daemon as a countermeasure for the problem that RuboCop starts slowly
The story of making it possible to build a project that was built by Maven with Ant
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
A memo that was soberly addicted to the request of multipart / form-data
Declare a method that has a Java return value with the return value data type
A collection of phrases that impresses the "different feeling" of Java and JavaScript
The story of making a game launcher with automatic loading function [Java]
Get the result of POST in Java
[Code] Forcibly breaks through the C problem "* 3 or / 2" of [AtCoder Problem-ABC100] with Java [Code]
Validate the identity token of a user authenticated with AWS Cognito in Java
The problem that the mount directory of Docker Desktop with WSL2 as the back end becomes strange in Windows Terminal (unsolved as of 2020/08/29?)
Measure the size of a folder in Java
Calculate the similarity score of strings with JAVA
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
[Java] Integer information of characters in a text file acquired by the read () method
Takes out the HashMap value and returns the result sorted by the value of value as a list.
Pass arguments to the method and receive the result of the operation as a return value
[Java] The problem that uploaded images are not updated due to the influence of cache
[Java] Cut out a part of the character string with Matcher and regular expression
CI the architecture of Java / Kotlin applications with ArchUnit
The story of making a reverse proxy with ProxyServlet
Monitor the internal state of Java programs with Kubernetes
Check the behavior of Java Intrinsic Locks with bpftrace
Experience of passing Java Silver as a new graduate
[Java] When writing the source ... A memorandum of understanding ①
Display the average value of the evaluation as a star
Output true with if (a == 1 && a == 2 && a == 3) in Java (Invisible Identifier)
Check the result of generic parameter inference with JShell
A survey of the Kubernetes native Java framework Quarkus
The story of making dto, dao-like with java, sqlite
Java desktop with the same front end as the WEB.
Replace only part of the URL host with java
A program (Java) that outputs the sum of odd and even numbers in an array
The story of building a Java version of Minecraft server with GCP (and also set a whitelist)
Java learning_Behavior when there is a method with the same name as a field with the same name in two classes that have an inheritance relationship