What’s the difference between int and integer?

Java is an almost pure object-oriented programming language, but basic data types are introduced for programming convenience. However, in order to operate these basic data types as objects, Java has introduced the corresponding wrapper class for each basic data type, and the wrapper class of int is integer. Since Java 5, the automatic boxing / unpacking mechanism has been introduced, so that the two can be converted to each other.

If the interviewer asks the difference between integer and int: it is estimated that most people will only say two points. Ingeter is the wrapper class of int, the initial value of int is 0, and the initial value of ingeter is null. But if the interviewer asks again, integer I = 1; int ii = 1; Is I = = II true or false? It is estimated that some people can't answer. If you ask others, it is estimated that more people will be confused. So I summarized them, hoping to be helpful to you.

(1) Integer is the wrapper class of int; int is the basic data type; (2) integer variables must be instantiated before they can be used; int variables do not need to; (3) integer is actually a reference to an object, pointing to the integer object of this new; int is the data value stored directly; (4) the default value of integer is null; the default value of int is 0.

(1) Since the integer variable is actually a reference to an integer object, the two integer variables generated through new are always unequal (because new generates two objects with different memory addresses). (2) when comparing the integer variable with the int variable, as long as the values of the two variables are equal, the result is true (because when the wrapper class integer is compared with the basic data type int, Java will automatically unpack it as int, and then compare it, which will actually become the comparison of two int variables) (3) when the integer variable generated by non new is compared with the variable generated by new integer (), the result is false. (because the integer variable generated by non new () points to the object in the Java constant pool, while the variable generated by new integer () points to the new object in the heap, The addresses of the two in memory are different) (4) for two integer objects not generated by new, when comparing, if the values of the two variables are between - 128 and 127, the comparison result is true; if the values of the two variables are not in this interval, the comparison result is false, integer I = 128; integer J = 128; system.out.print (I = = J)// False reason for article 4: Java is compiling integer I = 100; When, it will be translated into integer I = integer valueOf(100)。 In the Java API, the valueof of integer type is defined as follows. Numbers between - 128 and 127 will be cached. When integer I = 127, 127 will be cached. When integer J = 127 is written next time, it will be directly fetched from the cache and will not be new= 127; if (i >= IntegerCache.low && i <= IntegerCache.high){ return IntegerCache.cache[i + (-IntegerCache.low)]; } return new Integer(i); }

(1) Basic data types include Boolean, byte, int, char, long, short, double and float; (2) reference data types include array, class and interface.

For the convenience of programming, basic data types are introduced. However, in order to operate these basic data types as objects, Java introduces the corresponding wrapper class for each basic data type. The wrapper class of int is integer. Since the beginning of Java 5, the automatic packing / unpacking mechanism has been introduced, so that the two can be converted to each other.

//The above statement uses < a href=“ https://www.jb51.cc/tag/zidong/ " target="_ Blank "class =" Keywords "> automatic packing of < / a >: resolved as: integer num = new integer (9);

//Implicit in the calculation is < a href=“ https://www.jb51.cc/tag/zidong/ " target="_ Blank "class =" Keywords "> automatic < / a > unpacking

//Number within - 128 ~ 127 integer Num3 = 9; Integer num4 = 9; Sy stem < / a >. Out. Println (Num3 = = num4); / / true}} reason for analysis: it comes down to Java's design of automatic packing and unpacking of integer and int, which is a mode: call meta mode (flyweight). To increase the reuse of simple numbers, Java defines that during automatic boxing, for values between – 128 and 127, after they are boxed as integer objects, they will be reused in memory, and there will always be only one object. If the value between – 128 and 127 is exceeded, the boxed integer objects will not be reused, that is, they will be reused every time they are boxed Create a new integer object.

When assigning an int value to an integer object, the static method valueof integer class will be called. The source code is as follows: = 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache. cache[i + (-IntegerCache.low)]; return new Integer(i); } Integercache is the internal class of integer. The source code is as follows: option control* During VM initialization, Java lang.Integer. IntegerCache. High attribute * can be set and saved in private system attribute * / private static class integercache {static final int Low = - 128; static final int high; static final integer cache []; static{

reference resources:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>