Java data type and type conversion, java data type conversion

Source: Internet
Author: User

Java data type and type conversion, java data type conversion

Java data types include basic data types and reference data types.

  

 

When a Java program is running, it needs to allocate space in the memory. To improve the computing efficiency, we divide the space into different regions, because each region has a specific data processing and memory management mode.

   
Stack: the variables of some basic types defined in the function and the referenced variables of objects are allocated in the stack memory of the function. In Windows, the stack is a data structure extended to a low address,
Is a continuous memory area. This statement indicates that the stack top address and the maximum stack capacity are pre-defined by the system. In WINDOWS, the stack size is 2 MB
1 M, which is a constant determined during compilation). If the applied space exceeds the remaining space of the stack, overflow is displayed. Therefore, the space available from the stack is small.

Heep: The heep memory is used to store objects and arrays created by new. The heap is a data structure extended to the high address and is a discontinuous memory area. This is because the system uses linked lists for storage.
The idle memory address is not consecutive, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. As you can see,

The space obtained by the heap is flexible and large.

Method Area: used to store class information.

Local method Stack: supports native underlying methods of C/C ++.

PC register: used by the CPU.

 

The relationship between heap and stack: When a variable is defined in a code block, Java allocates memory space for the variable in the stack. When the variable scope is exceeded,
Java will automatically release the memory space allocated for the variable, and the memory space can be used for another use immediately.

After an array or object is generated in the stack, you can define a special variable in the stack so that the value of this variable in the stack is equal
The first address of an array or object in the heap memory. The variable in the stack becomes the referenced variable of an array or object. Reference Variables
When an array or object is named, you can use the reference variable in the stack in the program to access the array or object in the heap.
The referenced variable is a common variable. It is assigned to the stack during definition. The referenced variable is released after the program runs out of its scope. Arrays and objects are allocated in the heap,
Even if the program runs outside the code block where new statements are used to generate arrays or objects, the memory occupied by arrays and objects is not released,
Arrays and objects become junk only when no referenced variable points to it. They cannot be used, but still occupy the memory space,
The garbage collector collects (releases) the garbage collector at an uncertain time ).

The variables in the stack point to the variables in the heap memory. This is the pointer in Java!

 

Data Type Conversion

  Conversions of data types include implicit conversions and display conversions. The difference lies in the range size (for example, the range of int-2 ^ 16 ~ 2 ^ 16-1. Note that the conversion direction is not the byte here, that is, the upward and downward transformations.

Implicit conversions refer to upward conversions and are automatic conversions, for example:

                int a=50;             long b=a;            

 

Display type conversion refers to downward transformation, which is a forced conversion, indicating that a large range is converted to a small range of data types, such:

         long a=50;            int b=(int) a;

 

  

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.