The difference between Java int and integer

Source: Internet
Author: User
Tags wrapper

Today, the difference between an int and an integer is that it is not clear what you know about the problem, and some of the concepts are still wrong, so make a summary of the difference between int and 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

An int is a basic data type and an integer is a complex data type (that is, a class), so a variable of the int class is 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);(integer is a class), but because of the automatic boxing and unpacking, it can also be used for 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.

There are eight basic data types in Java: Character type char, Boolean type Boolean, and numeric type Byte, short, int, long, float, double; each basic data type has a wrapper class,

The corresponding packing class is character,booblean,byte,short,integer,long,float,double

For example, when you need to put things into the arraylist,hashmap, such as int,double this basic type is not put in, because the container is loaded with object, this is the need for these basic types of packaging class.

Here's a snippet of code to illustrate the following:

  

1 /**2 * difference between int and integer types3 * 2016/5/44  * 5  */6  PackageCN. java_7;7  Public classCampare {8      Public Static voidMain (string[] args) {9Integer A =NewInteger (127), B =NewInteger (128);Ten         intc = 127, D = +, DD = 128; OneInteger e = 127, EE = 127, F = $, FF = 128; A          -System.out.println (A = = B);//false because A, B is a new object with a different address, so false -System.out.println (A = = c);//true A is automatically unboxing to int type theSystem.out.println (A = = e);//false to point to different address A is new out of -System.out.println (E = = c);//true E is automatically unboxing to int type -System.out.println (E = = ee);//true in the range of 128 to 127, pointing to the same piece of address area -          +System.out.println (b = = f);//false point to the address different B is new out of -System.out.println (f = = d);//true F automatic unpacking to int type +          ASystem.out.println (f = = ff);/*false points to not the same piece of address area in the integer type, where 128 to 127 is the same area address, the number at It's another space for storage.*/ -System.out.println (d = = dd);//true does not explain -     } -}

The difference between Java int and 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.