The difference between integer and int and the explanation of the integer. values () method, nonintegervalues
Disclaimer: This article is reposted by the blogger. For the original article address, see the end of this article.
Knowledge Point 1: difference between integer and int
/*
* Int is one of the eight primitive data types provided by java. Java provides encapsulation classes for each original type, and Integer is the encapsulation class provided by java for int. The default value of int is 0,
* The default value of Integer is null.
*, That is, the Integer value can distinguish between the unassigned value and the value 0, and the int value cannot express the unassigned value. For example, to express the difference between not taking the test and the score 0
*, Only Integer can be used.
*. In JSP development, the default Integer value is null. Therefore, when an el expression is used in the text box, the value is a blank string, and the default value of int is 0, so when the el expression is displayed in the text box
*, The result is 0. Therefore, int is not suitable for the form data type of the web layer.
* In Hibernate, if the OID is defined as an Integer type, Hibernate can judge whether an object is temporary based on whether its value is null.
* If the OID is defined as int type, you also need to set its unsaved-value attribute to 0 in the hbm ing file.
* In addition, Integer provides multiple Integer-related operation methods. For example, to convert a string to an Integer, Integer also defines constants that represent the maximum and minimum values of an Integer.
*/
Knowledge Point 2: integer. values ()
| 123 |
System.out.println(Integer.valueOf("127")==Integer.valueOf("127"));System.out.println(Integer.valueOf("128")==Integer.valueOf("128"));System.out.println(Integer.parseInt("128")==Integer.valueOf("128")); |
Why is the first result returned?trueThe second Judgment returnsfalse?127And128What is the difference I don't know? (Except127Less128...)
Also, why is the third judgment returned?true?
I read the answer to another related question, but I still don't know when they will return true, and why the second one will returnfalse.
Answer #1:
Integer.valueOf(String)There is indeed an unusual behavior.
valueOfReturnsInteger(Integer) object, when the processed string is in-128And127Objects returned between (including boundaries) are pre-cached. This is why the first line of call returnstrue-127
This integer object is cached (so twice)valueOfReturns the same object.falseBecause128Because it is not cached, a new integer object is generated every time it is called,
Therefore128Integer objects are different objects.
Important:You need to know that in the above comparison, what you actually compare isinteger.valueOfThe returned object references, so when you compare integer objects outside the cache, equal judgment will not returntrueEven if you
UploadvalueOfThe values are equal and useless. (Like in the second row)Integer.valueOf(128)==Integer.valueOf(128)). To make this judgment returntrue, You need to useequals()Method.
parseInt()The returned result is not an integer object, butintBasic elements. This is why the Last Judgment returnstrueIn the third row's judgment, when the judgment is equal, the actual comparison is128 == 128,
So it must be equal.
Let's talk about the difference in the third comparison so that the result is different from that in the second comparison:
An unboxing conversion (A comparison-based conversion that converts an object's reference to its corresponding atomic type) occurs in the comparison of the third row. Because the comparison operator uses==At the same time, both sides of the equal sign
OneintAndIntegerObject reference. In this caseIntegerObject is further convertedintThe value is equal to the value on the left.
Therefore, after the conversion is complete, you actually compare two atomic integer values. This conversion is exactly what you expected when comparing two atomic types, so you finally compared128Equal128.
Answer #2:
IntegerClass has a static cache that stores 256 specialIntegerObject-each object corresponds to a value between '-128 and 127 respectively.
With this concept, you can know the difference between the above three lines of code.
A newIntegerObject.
| 1 |
Integer.parseInt("123"); |
ReturnsintValue.
| 1 |
Integer.valueOf("123"); |
This situation is more complex than others. First, the string is parsed. Then, if the parsed value is-128And127, The object is returned from the static cache. If this range is exceeded
Integer()Method and pass the parsed value as a parameter to get a new object.
Now let's take a look at the three expressions in the question.
| 1 |
Integer.valueOf("127")==Integer.valueOf("127"); |
The above expression returnstrueBecauseIntegerThe value is obtained twice from the static cache, and the expression returns the result of comparing the object with itself. Because there is only one Integer object, the returned result istrue.
| 1 |
Integer.valueOf("128")==Integer.valueOf("128"); |
The above expression returnsfalseBecause128No static buffer exists. Therefore, each time the equality is determined, a new one will be created on both sides of the equation.IntegerObject. Because twoIntegerObjects are different, so==Only Equations
Returns only when the two sides represent the same object.true. Therefore, the equation above returnsfalse.
| 1 |
Integer.parseInt("128")==Integer.valueOf("128"); |
The above expression compares the original on the left.intValue128And the newIntegerObject. But becauseintAndIntegerTherefore, before the comparison, Java
So the last step isintAndintComparison between values. Because128Equal to itself, so returntrue.
Difference between integer and int