Integer n = new Integer (1) and the difference int n = 1

First, from the most basic to start, int belong to the basic data types, object classes belonging to the package Integer, both as defined in itself is different, but the two are directly related, in java, the core is an object, all things are objects, each of the basic data type corresponding to the class, int corresponding class is Integer, char corresponding class is Character, float corresponding class is Float like. In Integer n = new Integer (1), n ​​is an object inheritance are as follows:

java.lang.Object

  Inheritorjava.lang.Number

      Inheritorjava.lang.Integer

 

Therefore, n has a series of properties and methods.
For int n, n is an integer variable, do not have any attributes and methods.
However, both can be transformed into each other, may be used int Integer becomes intValue () method, but may also be packaged as an int Integer object.
 
 
Sometimes we see Integer n = 1; such a code, so this problem involves the automatic sealing, unpacking, and are not repeated here

Guess you like

Origin www.cnblogs.com/zhuyeshen/p/12103815.html