Comparison and difference between Java int and integer

Difference between Java int and integer:

The difference between int and integer is the difference between the basic data type and its wrapper class:

Int is a basic type that stores values directly, while integer is an object that points to the object with a reference

1. Data types in Java are divided into basic data types and complex data types

Int is the former and integer is the latter (that is, a class); therefore, when the class is initialized, the variable of int class is initialized to 0. And the variable of integer is initialized to null

2. During initialization:

  int i =1; Integer i= new Integer(1); (think of integer as a class); But with automatic packing and unpacking

This makes it possible to use the integer class: integer I = 1;     

Int is a basic data type (a trace left by process oriented, but a useful supplement to Java). Integer is a class, an extension of int, and defines many conversion methods

Similar are: float; double Double; String, string, etc., but also provides some other constants and methods that are very useful when dealing with int types

For example, when you need to put things into ArrayList and HashMap, built-in types such as int and double can't be put in. Because containers are loaded with objects, you need the outer classes of these built-in types.

Each built-in type in Java has a corresponding outer class.

The relationship between int and integer in Java is more subtle. The relationship is as follows:

  1. Int is the basic data type;

  2. Integer is the encapsulation class of int;

  3. Both int and integer can represent a numeric value;

  4. Int and integer cannot be used with each other because they have two different data types;

Examples

In addition, int is not supported during generic definition: for example, list < integer > List = new ArrayList < integer > (); Yes, and list < int > List = new ArrayList < int > (); No

In short: if we define a number of type int, which is only used to perform some operations of addition, subtraction, multiplication and division, or is passed as a parameter, we can directly declare it as the basic data type of int

If you want to process objects as objects, you need to use integer to declare an object. Because Java is an object-oriented language, it can provide many methods of conversion between objects when declared as objects, which is similar to some common methods

Methods. I think Java is an object-oriented language. When we declare a variable, we'd better declare it in object format, which is more conducive to your understanding of object-oriented.

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>