Differences between the base data type and the reference data type 2015-07-30

Source: Internet
Author: User
Tags float double

Baidu Search Two articles:

First article

Data types in Java fall into two main categories: basic data types and reference data types

Basic data types, including numeric, character, and Boolean.

Numeric: 1) Integer: Byte 1 bytes; short 2 bytes; int 4 bytes; a long 8 bytes.

2) Float: Float 4 bytes, double 8 bytes, decimal and hexadecimal notation can be used, where the hexadecimal representation method can only use scientific notation, For example: 0X1.2P3, said yes is 1 times 16 plus 2 times 16-1 time Square and multiplied by 2 three times; the default type of the float type is double, if float float is declared, add F or F to the value, for example: float f1 = 3.14F; Otherwise, the accuracy error is reported.

Character type: Char is encoded using UNICOD's 16-bit encoding method.

Boolean type: True,false;

Reference data type: Class, interface type, array type, enumeration type, annotation type;

The difference between the base data type and the reference data type is primarily stored in the same way:

When the base data type is created, it divides the memory on the stack and stores the value directly on the stack;

Reference data type When the frame is being used, the first thing to do is to allocate a piece of memory to its reference (handle) on the stack, and the object's specific information is stored in the heap memory, and then the reference above the stack points to the address of the object in the heap.

For example: I have a class mydate, which has properties day,mouth,year, etc., there are construction methods (with parameters);

Now create an object for it mydate d1 = new MyDate (8,8,2008);

The specific creation process in memory is:

1) First allocate a piece of space in the stack memory bit its d1;

2) Then allocate a space for the MyDate object in the heap memory and set the initial value 0,0,0 for its three properties;

3) According to the definition of the property in the class MyDate, the three attributes of the object are assigned operation;

4) Call the construction method and assign a value of 8,8,2008 to three properties (note that there is no connection between D1 and MyDate objects at this time)

5) assigns the address of the MyDate object in the heap memory to the D1 in the stack, and through the handle D1 can find the concrete information of the object in the heap.

Hehe, the creation of reference data types is a very complicated process ...

Second article

┏ numeric ━┳━ integer type: byte short int long
┏ basic data Type ━━┫┗━ floating-point type: float double
┃┣ character type: Char
Data Type ╋┗ Boolean: Boolean
┃┏ classes (Class)
┗ Reference data Type ━━╋ Interface (interface)
┗ Arrays (Array)

Java data types are divided into basic data types (raw data types) and reference data types;

Basic data type:

Basic data types are basically very small and particularly simple variables, if you create objects with new, because the object created by new is stored in the heap, you know, because the flexibility of the heap determines that it is less efficient than the stack, so for 8 basic data types, Java does not use the new way to create a handle, Instead, an automatic variable with a non-handle is created directly on the stack, which holds the specific value, so it can be accessed efficiently.

Reference data type:

The reference data type is a pointer to the three-C + +, which points to the object entity in a special way (the specific value), when the variable is declared without allocating memory, and when new, a handle is stored in the stack for efficient reference, and its object entity (the specific value) opens up a chunk of memory in the heap. The entity is called through a handle in the stack.

Differences between the base data type and the reference data type 2015-07-30

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.