The difference between a "go" Java int and an integer

Source: Internet
Author: User

The difference between a Java int and an integer

The difference between an int and an integer is, in large terms, 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, pointing 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;integer i= new Integer (1);(to look at Integer as a class), but due to automatic boxing and unpacking (http://www.cnblogs.com/ssslinppp/p/4651368.html),

Makes it possible to use the Integer class: integer i= 1;

int is the basic data type (the trace left for the process, 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 int other constants and methods that are useful when dealing with 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> ();

All in all: if we define a number of type int, just for some subtraction operation or as a parameter, then you can declare it as an int base data type directly, but if you want to like

object, 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, it is better to declare a variable as an object format, which is more beneficial to your understanding of the object opposite.


Source: >  

From for notes (Wiz)

The difference between a "go" Java int and an integer

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.