So let’s look at a little bit of code, pretty simple, right

Execute:

Contrary to what most people think, many people are still struggling to decide whether all is true or all is false.

However, one is true and one is false. Why is this?

Some people don’t think at all, just open Baidu search to find the answer (all of them are chief software engineers of Baidu)

The.class file is generated for the JVM to load and execute, so we went to the.class file and decomcompiled it. When we compiled our code, the compiler mischievously (and slyly) added the valueOf method to the variable we declared, and the code looked like this:

Look at the implementation of the valueOf method

If the value is in the cache, the value is returned. If the value is not in the cache, a new object is returned. Consider the IntegerCache class:

This is an internal static class that can only be accessed inside the Integer class, which, when initialized, loads the JVM’s configuration, initializes the cache array with the configured value if any, and caches values between -128 and 127 otherwise.

Take a look at our previous code:

Conclusion: When comparing the values of two Integer objects, we must use equals instead of ==. There is no overloaded operator in Java, especially if you are moving to Java from other languages. I’ve covered equals in detail in other articles, Portal: Talk about Equals in Java

Consider the result of executing the following code:


If you liked this series, please give it a thumbs up or share it with me. Your support will keep me going, and leave a comment in the comments section where you can find out what you want to know.

Previous: Before the column – Java stuff

Java strings – Java strings

Reprint unlimited welcome, but please indicate “author” and “original address”. Please keep this paragraph in the article, thank you for your respect for the author’s copyright. For commercial reprint or publication, please contact the author for authorization.