Difference between int and integer in Java

Source: Internet
Author: User

Int Is the basic type and directly saves the value
Integer is an object, pointing to this object with a reference

1. Data Types in Java are classified into basic data types and complex data types.
Int Is the former> integer is the latter (that is, a class)
2. During initialization>
Int I = 1;
Integer I = new INTEGER (1); (consider Integer as a class)

Int Is the Basic Data Type (trace of process orientation, but it is a useful supplement to Java)
Integer is a class that is an extension of int and defines many conversion methods.
Similarly, float, double, string, and so on.

For example, when you want to put things in arraylist and hashmap, the built-in types such as int and double cannot be put, because the containers are loaded with objects, this requires these built-in types of override classes.
In Java, each built-in type has a corresponding override class.

Java provides two different types: reference type (or encapsulation type, warpper) and original type (or built-in type, primitive ). Int Is the original data type of Java, and integer is the encapsulation class provided by Java for int. Java provides encapsulation classes for each original type.
Original Type encapsulation class
Boolean
Char character
Byte byte
Short short
Int integer
Long long
Float float
Double double
The behavior of the reference type and the original type is completely different, and they have different semantics. The reference type and the original type have different features and usage, they include: size and speed problems, which type of data structure is stored, the default value specified when the reference type and original type are used as instance data of a class. The default value of the instance variables referenced by the object is null, and the default value of the original type of instance variables is related to their types.

Int Is the basic type, and (INT) (math. Random () * 100) is a number. You can add, see, and divide. Integer is a class, so new INTEGER (temp) is an object. You can use the class method of integer. For example, you can use intvalue () to return the value of this int.

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.