int and Integer in Java

Source: Internet
Author: User

1. The large aspect of int and integer is the difference between the basic data type and its wrapper class:

int is the basic type, the value is stored directly, and the integer is the object, with a reference to that object, which is a complex data type.

2. When initializing:

The variable of int is initialized to NULL as a variable of 0,integer.

int i =1;integer i= new integer (1); integer is a class that is an extension of int that defines a number of conversion methods.

3. Also similar are the float float;double double;string string, and int other constants and methods that are useful when handling types.

4. Note: When you need to store something in the hashmap,arraylist, like int,double, this type is not put in, because the container is loaded with object, which requires these built-in type of outer covering class.

5. The relationship between the two:

Integer is the encapsulation class of int;

Both can represent a value, but they are not interoperable because the two data types are different;

For example:

ArrayList a=new ArrayList ();

int i=10;

Integer n=new integer (1);

A.add (i);//Not allowed

A.add (n);//Can

Note: int is not supported when defining a generic type.

In summary: int knowledge is used to do some subtraction operations or to pass as parameters. If you want to work like an object, you need an integer to declare an object.

int and Integer in Java

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.