== and equals the difference between contact and face questions about int and integer

First, comparison == number of basic data types when the comparison is a value, and compare with two objects is the == number comparing address values ​​of the two objects;

That equals () method do? We can know by looking at the source code, equals () method exists in the Object class, because the Object class is the class of all direct or indirect parent class, meaning that all the class equals () methods inherited from the Object class, and by we found the source code, the equals () method of the Object class is dependent on the underlying == number, then all does not override equals () method of the class, call equals () method and the results actually use == number, just as comparing the value of the address value, however, all classes offered in Java, the majority of classes override equals () method, such as string, integer, etc., equals rewritten () method of two objects are generally more .

The difference between int and Integer

1, Integer is a wrapper class int, int is a java basic data types 
can be used after 2, Integer variables to be instantiated, but does not require the variable int 
3, Integer object is actually referenced, when a new Integer, actually generates a pointer to the object; and int is directly stored data value 
4, Integer default is null, a default value is 0 int

 About int Integer and comparison 
1, the Integer variable is actually a reference to an Integer object, so the two by generating new Integer variables are not always equal (because of a new generation of two objects, which is different memory addresses) .

 

2, when comparing int Integer variables and variables, as long as the two variables are like, the result is true (when compared to packaging because int Integer and basic data types, java int is automatically unpacked, and then compared, in fact it becomes comparing two int variables) 

 

3, new generation of non-variable and new Integer Integer () when comparing the generated variable, the result is false. (Because the new generation of non-variable points Integer object java constant pool, the new Integer () variable generated new objects on the heap point, the two different addresses in memory)

 

4, two non-Integer object for the new generation, when compared, if two variables interval between -128 to 127, the comparison result is true, if the value of two variables is not in this range, the comparison result It is false.

 

 because

java compiled Integer i = 100; when translated will become Integer i = Integer.valueOf (100) ;, and java definition of the API type Integer valueOf follows:

 

java to the number of -128 to 127, is cached, Integer i = 127, the cache 127 will next write Integer j = 127, they will taken directly from the cache, it will not be a new
----------------
Disclaimer: This article is the original article CSDN bloggers "candied sweet darling child", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/loveliness_peri/article/details/81108524

Guess you like

Origin www.cnblogs.com/sbclmy/p/11441491.html