Home > Java > javaTutorial > The difference between integer and int and the detailed explanation of integer.values() method

The difference between integer and int and the detailed explanation of integer.values() method

巴扎黑
Release: 2017-06-26 10:20:01
Original
1572 people have browsed it

Statement: This article is a reprinted article by the blogger. The original address can be found at the end of the article.

Knowledge point 1: The difference between integer and int

/*
* int is the 8 primitive types provided by java One of the data types. Java provides wrapper classes for each primitive type. Integer is the wrapper class provided by java for int. The default value of int is 0,
* and the default value of Integer is null
*, that is, Integer can distinguish the difference between unassigned value and value 0, int It is impossible to express the unassigned situation. For example, if you want to express the difference between not taking the exam and the exam score being 0
*, you can only use Integer
*. In JSP development, the default value of Integer is null, so when the el expression is used to display it in the text box, the value is a blank string, and the default value of int is 0, so when the el expression is used to display it in the text box
* , the result is 0, so int is not suitable as the form data type of the web layer.
* In Hibernate, if the OID is defined as an Integer type, then Hibernate can determine 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 mapping file.
* In addition, Integer provides multiple integer-related operation methods, such as converting a string into an integer. Integer also defines constants that represent the maximum and minimum values ​​of integers.

*/

Knowledge point 2: Detailed explanation of integer.values() method

Why does the first judgment return true and the second judgment returns false? Is there any difference between 127 and 128 that I don’t know? (Of course, except 127 is less than 128...)

Also, why does the third judgment return Got true?
I read the answers to another related question, but I still don’t know when they return true and why the second judgment returns false.

Answer #1:

Integer.valueOf(String) does have an unusual behavior.

valueOf will return an Integer (integer) object when the string being processed is between -128 and 127 (inclusive boundaries), the returned object is pre-cached. This is why the call in the first line returns true-127

This integer object is cached (so twice valueOf The same object is returned) - the call in the second line returns false because 128 is not cached, so each call will generate a new integer object,

So the two 128integer objects are different objects.

The important thing is that you need to know that in the above comparison, what you are actually comparing is the object reference returned by integer.valueOf, so when you compare outside the cache When an integer object is used, the equality judgment will not return true, even if you

pass a valueOf value that is equal, it will be useless. (Like Integer.valueOf(128)==Integer.valueOf(128)) in the second line. If you want this judgment to return true, you need to use the equals() method.

parseInt() returns not an integer object, but a int type basic element. This is why the last judgment will return true. In the third line of judgment, when judging equality, the actual comparison is 128 == 128,

so It must be equal.

Let’s talk about a little difference in the third comparison, which makes its result different from the second comparison:

an unboxing conversion (a conversion during comparison, which converts the The conversion of the object reference to its corresponding atomic type) occurs in the comparison on line 3. Because the comparison operator uses == and there is a

int

type and a Integer object reference on both sides of the equal sign . In this case, the Integer object returned on the right side of the equal sign is further converted into an int value before being equal to the left side.

So after the conversion is completed, what you are actually comparing are two atomic integer values. This conversion is exactly what you'd expect to see when comparing two atomic types, so you end up comparing 128 equals 128.

Answer #2: The

Integer class has a static cache that stores 256 special Integer objects - each object corresponds to a value between `-128 and 127.
With this concept, you can know the difference between the above three lines of code.

##1
2
3
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"));

Shows that a new Integer object is created.

##1
##new Integer(123);
##1
##Integer.parseInt(
"123");
##Return after parsing the string A
int
value.

##1##This situation is better than others It's a little more complicated. String parsing is done first, and then if the parsed value is between -128 and
#Integer.valueOf(
"123"
);
127
, the object is returned from the static cache. If it exceeds this range, the

Integer() method will be called and the parsed value will be passed in as a parameter to get a new object. Now, let’s look at the 3 expressions in the question.

##1
#Integer.valueOf()==Integer.valueOf(, because the value of Integer is fetched 2 times from the static cache, the expression Returns the result of comparing the object with itself. Because there is only one Integer object, the return result is
"127"
"127"
);
The above expression returns
true
true
.

##1

#Integer.valueOf("128""128"
)==Integer.valueOf(
);

The above expression returns false because 128 no static buffer exists. So every time when equality is determined, new Integer objects will be created on both sides of the equation. Since the two Integer objects are different, == will return

true

only when both sides of the equation represent the same object. Therefore, the above equation returns false.

##1
##Integer.parseInt(
"128")==Integer.valueOf("128");
The above expression compares the original
int

value 128 on the left with the newly created Integer on the right Object. But because the comparison between int and Integer is meaningless, Java will automatically unbox Integer before comparison, so the final thing is # Comparison between ##int

and

int values. Since 128 is equal to itself, true is returned.

The above is the detailed content of The difference between integer and int and the detailed explanation of integer.values() method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Previous article:The functions and differences between the keywords volatile and synchronized in Java Next article:java之Spring
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template