Conversion between Integer and int in Java

This article is reproduced from: http://isnb.blog.163.com/blog/static/194111114201061334935457/

int to Integer:

int a=3;

Integer A=new Integer(a);

or:

Integer A=Integer.valueOf(a);

Integer to int:

Integer A=new Integer(5);

int a=A.intValue();

As for Integer.parseInt(String str), it converts String type to int type.

      The int type is placed in the stack space, and the Integer is placed in the heap space as an object;

      Int is a basic type, not a class. In order to comply with object-oriented programming, the Integer class appeared later, which encapsulates int.

      int is not an object, it is a primitive data type of java, and its default value is 0.

      Integer is an object, it has its own method, the default value is NULL.

      The purpose of implementing this kind of object wrapping is mainly because the class can provide the necessary methods to realize the conversion between the numerical value of the basic data type and the printable string, as well as some other utility methods; In addition, some data structure library classes Objects can only be manipulated, not variables of basic data types. Wrapper classes provide a convenient way to convert basic data types into equivalent objects that can be processed using data structure library classes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325906285&siteId=291194637