The difference between Java advanced Java int and integer

Source: Internet
Author: User

the difference between Java advanced 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 ( starting with jdk1.5), the integer class can also be used: 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.

Also similar are the float float;double Double;boolean Boolean, 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 the similarities and differences between basic types and packaging types

1. In Java, everything is object, but the eight basic types (Char,byte,int,double,float,short,long,boolean) are not objects.

2, the way of declaring different, the basic type does not need to be created by the new keyword, and the package type needs the new keyword.

3, storage mode and location of the difference, the basic type is the value of the direct storage variables can be stored in the stack efficient access, the package type needs to point to the instance by reference, the specific instance is saved in the heap.

4, the initial value is different, the initial value of the package type is NULL, the initial value of the base type depends on the specific type, such as the initial value of the int type is 0 (integer: including Int,short,byte,long, the initial value is 0), the Boolean type is False, Floating-point: float,double, initial value is 0.0, character: char, initial value is a space, that is, "", if the output, on the console is not see the effect.

5, the use of different ways, such as working with the collection class can only use the package type.

Read more about Java Advanced (34) How much do you know about integers and int? 》 Original address: 51958714

(RPM) The difference between Java advanced 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.