Chapter II Universal Objects (Java Language Foundation)

Source: Internet
Author: User

I. Object references (Reference)

Second, new object

    1. Existing type
    2. Custom type

Third, storage

    1. Register: Fastest, located inside the processor. But the number of registers is limited, by the compiler according to its own needs appropriate allocation of use, (for a programmer) is basically a sense of the existence.
    2. Stack: In general RAM (random-access memory, random-access RAM). The processor provides direct support through its pointer, which allocates new memory when the pointer moves back, and the pointer moves forward when the memory is freed. Efficiency is second only to register. The Java compiler is responsible for generating "control pointer Movement" program code, so it must be able to fully grasp the program in which it compiles "the actual size and survival time of all the data in the stack". This limits the flexibility of the program, and therefore, we can only store the object's reference in the stack, but we cannot place the generic Java object on it.
    3. Heap: Heap is a general-purpose memory storage space (in RAM) that is used to place all Java objects and, using the keyword new, allocates space from the heap. The space allocated by the heap has a high degree of elasticity. But allocating space from the heap is a lot more time than it takes to allocate from a stack.
    4. Static storage: exists in RAM, which holds data that has persisted during program execution. With the keyword static, you can set a specific member of an object to be static, but the Java object itself is never willing to be placed in a static storage space.
    5. Constant storage space (Constant storage): Constant values are often placed directly in the program code or in read-only memory (Read-only memory,rom).
    6. Non-ram Storage: If the data is completely outside the program, the data can continue to exist even if the program is not executed, leaving the control of the program. Streamed objects (streaming objects) and persistent objects (persistent objects) are the main two examples. They can convert objects into forms that can be stored with other media and, if necessary, restore the stored data to a generic object that can be stored with RAM.

Iv. Basic type (basic data type)

    1. Do not use new to allocate space, produce a variable (automatic), not reference form, such variables directly hold the data value, and placed in the stack
    2. Each basic type has its counterpart (wrapper class), which is used to manipulate objects produced by the basic type, or to produce non-primitive objects representing the underlying type in the heap.

Five, high-precision numerical

    1. Java.math.BigInteger: Integer that supports arbitrary precision
    2. Java.math.BigDecimal: Provides fixed-point number of arbitrary precision

Vi. Arrays in Java

    1. An array of Java languages is actually a piece of memory. Java guarantees that the array will be initialized, and that the programmer's access to the array cannot be exceeded (at the cost of a little extra space for each of the arrays, and to check the value of the set index at execution time)
    2. When you produce an array of a stored object, what is actually generated is an array of storage references. Once this array is established, each of the references is automatically set to a special value. The value is represented by the keyword NULL. When Java sees a null value, it treats the referebnces as "not pointing to any object." Before using any reference, an object must be assigned to it, and if the value of a reference is NULL, an error occurs during the execution period. Arrays can also store basic types, and the compiler guarantees that the initialization action is inevitable: this time it will completely clear the memory of the array to zero.

Vii. scope (SCOPE)

Viii. Life cycle of objects

    1. Using new to produce a Java object, even if it leaves the scope (living space), the object still exists.
{  string s=new string ("a string");} /* End of Scope */

2. S This reference will disappear outside the scope, but the string object that s referred to earlier still continues to consume memory.

3. Java, however, has the so-called "garbage collector (garbage collector)" mechanism, which examines all objects produced through new, and when those objects are no longer referenced, the garbage collector frees the memory of those objects. So you

Do not care about memory recycling issues, as long as the concern about the production of objects is good. All objects will disappear automatically when you don't need them.

Ix. establishing a new data type (Class)

    1. Defining classes using the Class keyword
    2. Use the new keyword to produce such objects

X. Data members (fields) and functions (methods)

    1. A data member can be any type of object, as long as you communicate with it through reference. Data members can also be basic types (not reference)
    2. Each object holds the space used to store the data, and the different objects do not share data members with each other.
    3. Basic members (Boolean,int ... Default values for basic types, etc.)

Xi. method, parameter, return value

12. Static keyword

    1. No matter how many objects are produced, or if there are no objects, there is only one copy of the storage space for that particular data (example: Mian method in Java)
    2. A function is not tied to class object
    3. Static variables/methods can be called through objects
    4. Static variables/methods can be called through the class name
class statictest{  staticint i=0;  
public static void Main (stringp[] args) {

Statictest st_2=new statictest ();  System.out.println (ST_1.I==ST_2.I); True
System.out.println (ST_1.I==STATICTEST.I); True
}}

13. Compiling and executing

14. Annotations and in-built documents

XV, Javadoc

    1. NetBeans can automatically build Javadoc similar to official documents

16. Coding Style

Chapter II Universal Objects (Java Language Foundation)

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.