Java: The difference between a basic data type and a reference data type

Source: Internet
Author: User

I. Basic data types

byte: The smallest data type in Java, 8 bits in memory (bit), 1 bytes, value range -128~127, default value 0.

short: Shorter integer, 16 bits in memory, 2 bytes, value range -32768~32717, default value 0.

int: integer, which is used to store integers, which occupies 32 bits, or 4 bytes, within the value range -2147483648~2147483647, the default value of 0.

Long: An integer that occupies 64 bits in memory, or 8 bytes -2^63~2^63-1, the default value is 0L.

float: float, which occupies 32 bits in memory, or 4 bytes, that is used to store a number with a decimal point (the difference from a double is that the float type has a valid decimal point of only 6~7 bit), and the default value is 0.

Double: dual-precision floating point, used to store a number with a decimal point, which occupies 64 bits in memory, or 8 bytes, the default value of 0.

Char: Character type, used to store a single character, 16 bits, or 2 bytes, the value range 0~65535, the default value is empty.

Boolean: The Boolean type, which occupies 1 bytes, is used to determine true or false (only two values, that is, true, false), and the default value is False.

II. Reference data types

class , interface type, array type, enumeration type, annotation type.

Three, the difference

when the base data type is created, it is partitioned into a piece of memory on the stack, and the values are stored directly on the stack . For example:

var a = 10;

var B = A;

b = 20;

Console.log (a); 10 Value

b just holds the value of a. So the change of B value has no effect on a.

Demonstrates the process of assigning this basic data type:

When a reference data type is created, it first allocates a piece of memory to its reference on the stack, and the object's specific information is stored on the heap memory and then by the stack ( stack The reference above points to the address of the object in the heap (heap) . "Reference (Stack)--Object address (heap)"

For example, there is a class person, which has the attribute name, age, and the constructor method with the argument,

Person p = new person ("Tom", 20);

The specific creation process in memory is:

1. First allocate a memory space for p in the stack memory;

2. Assign a space to the person object in heap memory and set the initial value "" for its two attributes, 0;

3. According to the definition of the property in the class person, the object's two attributes are assigned operation;

4. Call the construction method and assign a value of two to "Tom", 20; (Note that there is no connection between p and person objects at this time);

5. Assign the address of the person object in the heap memory to p in the stack, and reference p to find specific information about the objects in the heap.

Iv. Correlation (heap area, stack area, constant zone, static zone)

Heap Area: typically distributed by programmers, released by The memory allocated by the malloc series function or new operator whose lifetime is determined by free or Delete. exists until it is released, until the program ends and is released by the OS.

Stack Area: The release is automatically allocated by the compiler, storing local variables (basic types of data and references to objects, but the objects themselves are not stored in the stack, but are stored in the heap). the contents of the stack exist only within the scope of the function, when the function is finished.

constant Area: a constant, a constant string (string) is placed here. Released by the system after the program is finished.

Static Zone: 1. Also called the method area, like the heap, is shared by all threads. 2. Store all the class and static variables. 3. The method area contains all the unique elements that are always in the program. 4. The contents of the static zone are present throughout the lifetime of the program and are allocated by the compiler at compile time.

Case study-- string memory allocation:

For strings, references to objects are stored in the stack, and if the compilation period has been created (defined directly in double quotes), it is stored in a constant pool, which is stored in the heap if the run-time (new) is determined . For a string equal to equals, there is always only one copy in the constant pool, with multiple copies in the heap.

1         String S1 = "China"; 2         String s2 = "China"; 3         String s3 = "China"; 4 5         New String ("China"); 6         New String ("China"); 7         New String ("China");

Here is an explanation of the yellow 3 arrows, for a string created by new (assuming "China"), will first go to the constant pool to find if there is already a "China" object, if not a constant pool to create this string object, and then create a constant pool in the heap in this "China" The Copy object of the object.

This is also the Youdao interview question: strings=newstring ("xyz"); How many objects are produced, one or two? If there is no "xyz" in a constant pool, it is two.

Reference URL: https://www.cnblogs.com/SaraMoring/p/5687466.html, https://www.cnblogs.com/bekeyuan123/p/7468845.html

Java: The difference between a basic data type and a reference data type

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.