In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "Why int==Integer is true in java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Why does int==Integer return true?
Let's look at the phenomenon first.
Execute the following code and output the result
Int a = 1; Integer b = 1; Integer c = new Integer (1); System.out.println (averse roomb); / / trueSystem.out.println (adept roomc); / / trueSystem.out.println (breadcrumb); / / false
The usual explanation for this is that = = compares values for basic types and references for reference types, that is, the memory address of the object to which it points. This explanation is correct, and there is no doubt that the result is false because both of them are reference types. But why is a reference type a basic type and a reference type when comparing values?
At this point, we might as well decompile the .class file compiled from the .java source file into the source code using a decompiler to see how the virtual machine handles astatb.
The .class file is decompiled using jd-gui:
Int a = 1 Integer Integer b = Integer.valueOf (1); Integer c = new Integer (1); System.out.println (a = = b.intValue ()); System.out.println (a = = c.intValue ()); System.out.println (b = c)
When you see this, you must all understand that when comparing the basic type a with the reference type b, the reference type b calls its own intValue () method to get the value of the int type actually represented by Integer, that is, a = = b.intValue () or two variables of type int for value comparison. In line with the above: = = for the basic type, it compares the value, and for the reference type, it compares the reference, that is, the memory address of the object to which it points.
How basic types and reference types are stored in memory
Speaking of which, explain why the values of the two reference types are the same but the references are different, and why the basic variables are value comparisons.
In fact, there is only one copy of the basic variable int an in memory, which is saved in the stack (holding variable data of basic types and references to reference types). The int values in Integer b and Integer c both point to the same int in the stack and will not re-create the same int value in the stack.
For Integer b and Integer c, in fact, the example is saved in the heap (save all the objects out of new), although the int value is the same, but there are two copies in the heap, each time new will open up a space in the heap to save the contents of new, so Integer b and Integer c are stored in two different memory spaces, so they point to different memory addresses.
For Integer b = 1; it is decompiled to Integer b = Integer.valueOf (1); and inside the valueOf () method is a call to new.
Integer.valueOf () source code in JDK:
Public static Integer valueOf (int I) {if (I > = IntegerCache.low & & I)
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.