The basics of my Java path

Source: Internet
Author: User
Tags array length

Allocation of memory when the program runs:

Register: This is the fastest save area, unlike all other save methods, it is stored inside the processor. Then, the number of registers is very limited, so the registers are allocated by the compiler as needed. We have no direct control over this, nor can we find any trace of the register in our own program.

Stack: resides in the general RAM (random access memory) area, but can be directly supported by its "stack pointer" for processing. If the stack pointer moves down, the new memory is created, and if you move up, the memory is freed. This is a particularly fast, particularly effective way to save data, second only to registers. When you create a program, the Java compiler must know exactly the "length" and "time of existence" of all data saved in the stack. This is because it must produce the appropriate code to move the pointer up and down. This restriction affects the flexibility of the program, so although Java data is stored on the stack-especially the object handle-the Java object is not placed in it.

Heap: A general-purpose memory pool (also in the Ram area) in which Java objects are saved. Unlike stacks, the "heap of Memory" or "heap" is most appealing because the compiler does not have to know how much storage to allocate from the heap, or how long the stored data will stay in the heap. As a result, you get more flexibility when you use heaps to save data. When you require an object to be created, you only need to compile the relevant code with the new command. When you execute the code, the data is saved automatically in the heap. To achieve this flexibility, there is a certain price to pay: allocating storage space in the heap can take longer.

Static storage: "Static" here means "in a fixed position" (also in RAM). During the run of the program, the data that is stored statically is always waiting to be called. The static keyword can be used to indicate that a particular element of an object is static. However, the Java object itself will never be placed in static storage space.

Constant storage: Normal values are directly inside the program code. It is safe to do so, because they will never change. Some constants need to be strictly protected, so consider placing them in read-only memory (ROM).

Non-RAM storage: If the data is completely independent of a program, then the program does not run is still available, and outside the control of the program. The two most important examples are "streaming objects" and "fixed objects". For streaming objects, the object becomes a byte stream, which is usually sent to another machine. For fixed objects, objects are saved on disk. Even if the program terminates, they can still keep their state intact.

High-precision numbers:

  Java 1.1 adds two classes for high-precision calculations: BigInteger and BigDecimal. Although they can be broadly classified as "wrapper" types, they do not have a corresponding "master type". These two classes have their own special methods that correspond to the operations we perform on the main type. That is, what can be done with int or float can be done for BigInteger and BigDecimal. Only method calls must be used and operators cannot be used. In addition, because of the more involved, so sacrifice speed, but in exchange for precision.

BigInteger supports integers of arbitrary precision. That is, we can accurately represent an integer value of any size without losing any information during a colleague's operation.

The BigDecimal supports vertex numbers of arbitrary precision. For example, it can be used for accurate currency calculations.

Array
The Java array requires that all array elements have the same data type. Therefore, the data type of an array element is unique within the same set of arrays.
Once the array is initialized, the space occupied by the array's memory will be fixed, so the length of the array cannot be changed. Even if the array element is emptied, the space occupied by it is preserved and the length of the array remains unchanged.
A Java array can store either basic types of data or data of reference types, as long as all array elements have the same data type
An array is a reference type variable, so when you use it to define a variable, it simply means that a reference type is defined, and that reference variable does not point to any valid memory. Therefore, you cannot specify the length of an array when you define it. Can only be used after an array has been initialized
Arrays in Java must be initialized before they can be used. The so-called initialization is to allocate memory space for array elements of arrays and assign values to each array element.
You cannot assign an initial value to an internal training, there are two forms of the initial value: One is automatically dispatched by the system, and one is assigned by the programmer;
There are two ways to initialize an array:
1, Static initialization: Initialization when the programmer explicitly specify the initial value of each array element, the system determines the length of the array
2. Dynamic initialization: When initializing a program ape specifies only the array length, and the system assigns the initial value
Do not use static initialization and dynamic initialization at the same time, i.e. do not indicate both the length of the array and the initial value for each array when initializing the array
Once the array is initialized, the array can be used, including the array element assignment, accessing the array element value, and getting the array length.
foreach Loop
Using foreach loops throughout arrays and collection elements without obtaining array and collection lengths, without accessing array elements and collection elements based on indexes, the Foreach loop automatically iterates through the array and each element of the collection

The basics of my Java path

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.