On the data types in Java

Source: Internet
Author: User
Tags garbage collection

The main content is from MIT6.031 software construction course and hit software construction course.

There are two types of data in Java, which are divided into basic data types (primitive types) and object data types (objects types). For example, types such as int, Float,boolean, and so on are basic data types, and string, Integer, BigInteger and so on are object data types.

Here are the similarities and differences between the two points:

The base data types are immutable (immutable), while some of the object data types are mutable (mutable), some are not. The base data type is stored in the stack (stack), and the object data type is stored in the heap, which is managed by the Java garbage collection mechanism (garbage collector). Basic data types often do not implement complex representations, whereas object data types can represent complex concepts in the form of generics. The basic data types typically come at a very small cost and object data types are expensive.

The object data type also has a wrapper data class (Boxed primitive) that is encapsulated by the basic data type, such as Boolean, Integer,short,long,float. It is mainly used in collection. Generally do not take the initiative to use the packaging class, because the cost of doing so is very high. Requires the compiler to make an automatic conversion.

For example:

list<integer> list = new arraylist<integer> (); List.add (1); List.add (2);/////////list.add ( Integer.valueof (1)); List.add (Integer.valueof (2));

We usually choose the first form of writing, but this is actually a more expensive way of writing. Since 1 and 2 are not objects at all, the compiler is automatically converted to reduce efficiency. The following wording is a more efficient way of writing.

On the data types in Java

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.