int integer difference in Java and mutual conversion __java

Source: Internet
Author: User
Tags numeric value wrapper

The difference between an int and an integer is, in large part, the difference between a basic data type and its wrapper class:

int is the basic type, the value is stored directly, and the integer is the object, and the object is pointed with a reference

1. Different types:

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

The int is the former and the integer is the latter (that is, a class), so the variable of the int class is initially 0 when the class is initialized, and the integer variable is initialized to null.

2. When initializing:

int i =1;integer i= new Integer (1);(to see Integer as a Class), but with automatic boxing and unboxing (http://www.cnblogs.com/shenliang123/archive/2012/ 04/16/2451996.html),

Make for the integer class also can use: integer i= 1;

int is the basic data type (process-oriented traces, but a useful addition to Java), Integer is a class, is an int extension, defines a lot of conversion methods

Similarly, float float;double double;string string, and other constants and methods that are useful for handling int types

For example: When you need to put something into a arraylist,hashmap, a built-in type like int,double is not going to fit in, because the containers are objects, and that's what you need these built-in types of outer-covering classes.

Each of the built-in types in Java has a corresponding outer-covering class.

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

1.int is the basic data type;

2.Integer is the encapsulated class of int;

Both 3.int and integer can represent a certain number;

4.int and integer cannot be used for each other because of their two different data types;

Give an example to explain

ArrayList al=new ArrayList ();

int n=40;

Integer ni=new integer (n);

Al.add (n);//Not allowed

Al.add (NI);//Can

Also, the generic definition does not support int: such as:list<integer> list = new arraylist<integer> (); can list<int> list = new arraylist< Int> ();

All in all: if we were to define a number of int type, just to perform some subtraction operations or pass as arguments, then you could declare it as an int basic data type, but if you want to be like

objects, it is necessary to declare an object with an integer, because Java is an object-oriented language, so when declared as an object, it provides many ways to convert between objects, with some common

The method. Since we think of Java as an object-oriented language, we'd better declare it as an object format when declaring a variable, which is more beneficial to your understanding of the object in the opposite direction.

The conversion between int and integer

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), the string type is converted to an 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, and in order to conform to object-oriented programming, an Integer class appears, and he encapsulates the Int.
int is not an object, is a Java raw data type, and its default value is 0.
An integer is an object, it has its own method, and the default value is null.

The purpose of implementing this object wrapper is primarily because classes can provide the necessary methods for implementing the conversion between the numeric value of the base data type and the printable string, as well as some other utility methods;
In addition, some data structure library classes can only manipulate objects, and do not support the basic data types of variables, packaging classes provide a convenient way to convert the basic data types to equivalent objects, so you can use the data structure library classes to handle.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.