What is the difference between Java int and integer?

Source: Internet
Author: User
Tags integer numbers

Int

int is one of the 8 primitive data types (Primitive Type:boolean, Byte, short, char, int, float, double, long) that we often call integer numbers. Although the Java language claims that everything is an object, the original data is an exception.

Integer

Integer is the wrapper class for int, which has a field of type int that stores data and provides basic operations such as math, int, and string conversions. With the introduction of auto-boxing and auto-unpacking in Java5, Java can be converted automatically according to the context, greatly simplifying the related programming.

About the value cache for integers

This involves another improvement in the JAVA5. The traditional way to build an integer object is to call the constructor directly and create a new object directly. However, according to the practice we found that most of the data operations are concentrated in a limited, small range of values, thus adding a static factory method in Java5 value of, when called it will use a caching mechanism, resulting in significant performance improvements. By Javadoc, the default value for this cache is 128 to 127.

Supplement 1

Primitive data types and Java generics do not work together, meaning that primitive types and generic cannot be mixed, so Java designs The auto-boxing/unboxing mechanism, which is actually primitive The invisible conversion mechanism between value and object, otherwise, if the mechanism is not, the developer must manually explicitly convert each time, this is much trouble. However, primitive value and object each have their own advantages, primitive value in memory is values, so find primitive value of memory location, you can get the value; Unlike object, it is reference. Find the memory location of object, but also according to reference to find the next memory space, to generate more IO, so the computational performance than primitive value difference, but object has the ability to generic, more abstract, solve business problems programming efficiency. So the Jvav designer's original intention is this: if the developer wants to calculate, use primitive value, if the developer wants to deal with business problem, should use object, adopt generic mechanism. Java has a auto-boxing/unboxing mechanism anyway, and there is no need for developers to notice anything. In order to compensate for the lack of object computing ability, the static valueof () method is also designed to provide a caching mechanism, which is considered as a remedy.

What is 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.