In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "Why in Java" 1000 is false and "100 is true". In daily operation, I believe many people have doubts about why "1000" is false and "100" is true in Java. The editor has consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer "why" in Java "1000" is for false and "100 is true"! Next, please follow the editor to study!
Public static void main (String [] args) {Integer integer1 = 100; Integer integer2 = 100; System.out.println (integer1 = = integer2); Integer integer3 = 1000; Integer integer4 = 1000; System.out.println (integer3 = = integer4);}
The output of the above code is as follows:
True false
Dynamic unpacking
First, before we introduce automatic unpacking, we modify the above code, change the type of variable from Integer to int, and re-execute the above code:
Public static void main (String [] args) {int integer1 = 100; int integer2 = 100; System.out.println (integer1 = = integer2); int integer3 = 1000; int integer4 = 1000; System.out.println (integer3 = = integer4);}
The output is as follows:
True true
So, what's the difference between int and Integer?
Int is the basic data type, while Integer is the wrapper class.
Because Java is an object-oriented language, there are many places where you need to use objects rather than basic data types, for example, elements must be object types in collections.
Therefore, the Java species provides the corresponding wrapper class for all basic types, and the wrapper class corresponding to int is Integer.
With basic data types and wrapper classes, you need to convert between them, and the process of converting the primitive type to the wrapper class is boxing, which, on the contrary, is called unboxing.
In Java SE5, in order to reduce the work of developers, Java provides automatic unpacking and automatic boxing functions.
Auto-boxing: the basic data type is automatically converted to the corresponding wrapper class.
Automatic unpacking: the wrapper class is automatically converted to the corresponding basic data type.
In our initial example, we used Integer integer1 = 100; to define and initialize a variable.
At this time, an automatic packing is involved. Because 100 is the primitive type int, and you need to assign it to the wrapper type object integer1, an automatic boxing occurs.
Integer integer1 = 100; in fact, it is Integer I = new Integer (10); because the automatic boxing function is provided in Java.
Caching mechanism of automatic boxing
Java SE's automatic unpacking also provides a cache-related function.
In order to save memory and improve performance, Java provides a caching mechanism for several wrapper types. In the process of automatic packing, some objects can be put into the cache to realize the reuse of objects.
Caching is supported by Byte, Short, Integer, Long, Character, etc.
For Integer, there is an inner class of IntegerCache inside. He caches objects with integer values between-128 and + 127. This cache is initialized the first time the Integer class is used. Later, instead of creating a new instance (in the case of automatic boxing), you can use the instance objects contained in the cache.
That is, when we automatically box a number with a value between-128 and + 127, instead of creating a new object every time, we pull a cached object directly from the cache.
= = what is compared
Now that we know about autoboxing and its caching mechanism, let's take a look at what exactly we compare when we use = = to compare two objects in our code.
Many people will think that for integer types, of course, the comparison is the value of integers, but it is not.
The = = in Java is the address of the object when comparing. If the address of two objects is the same, then true is returned, otherwise false is returned.
So, because there is automatic boxing, because there is a caching mechanism, because = = compares addresses.
So, when comparing the two Interger in-128to + 127, because the same object is fetched from the cache, the address of the object is the same, and true is returned.
For 1000, because it is not within the scope of the cache, a new object is created each time, so false is returned.
It is important to note, however, that there is a caching mechanism only when auto-boxing, which is independent of caching in the constructor, such as the following code:
Integer integer1 = new Integer; Integer integer2 = new Integer; System.out.println (integer1 = = integer2)
The output is
False
At this point, the study on "why the Java 1000 is false and the 100th is true" is over. I hope it can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.