Java Advanced (35) The difference between Java int and integer

Source: Internet
Author: User

Java Advanced (35) The difference between Java int and integerObjective

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

An int is a primitive type that directly saves a value, and an integer is an object that points to the object with a reference.

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

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

2. When initializing:

int i = 1;

The integer i= new Integer (1);(to look at integer as a class), but because of the automatic boxing and unpacking ( beginning with jdk1.5), Makes it possible to use the Integer class: integer i= 1;

int is a basic data type (a process-oriented trace, but a useful addition to Java), an Integer is a class, an int extension, and a lot of conversion methods are defined.

Similar to this: float float;double double;string string, and other constants and methods that are useful when working with int types

For example: when it is necessary to put something into the arraylist,hashmap, the built-in type such as int,double is not put in, because the container is loaded with object, which is the need of these built-in type of outer covering class.

Each of the built-in types in Java has a corresponding overlay 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 encapsulation class of int;

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

4.int and integer cannot be interoperable because they have two different data types;

Examples Show

ArrayList al=new ArrayList ();

int n=40;

Integer ni=new integer (n);

Al.add (n);//Not allowed

Al.add (NI);//Can

and the generic definition does not support int: such as:list<integer> list = new arraylist<integer> (); Yes list<int> list = new arraylist< Int> (); No.

All in all: if we define a number of int, just for some subtraction operation or as an argument, it can be declared as an int base data type directly, but if you want to handle it like an object, you declare an object with an integer. Because Java is an object-oriented language, it can provide many ways of converting between objects when declared as objects with some common methods. Since we think of Java as an object-oriented language, it is better to declare a variable as an object format, which is more beneficial to your understanding of the object opposite.

Note For more details, see the Java Advanced (34) How much do you know about integers and int? 》 American and American pictures


Java Advanced (35) The difference between Java int and integer

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.