Java study Note 1: object and storage

Source: Internet
Author: User

1. Use references to operate on objects

Java is an object-oriented programming language. Everything can be viewed as an object and operations on the object are completed through reference.

// String is the object, and s references String s to this String object;

Note: We use object references to manipulate object data. If the object is not associated with data (for example, it is not initialized), a null reference will occur, and a null pointer exception will be reported: java. lang. nullPointerException

2. Data Storage

(1) Register: located inside the CPU, which is the fastest processing area, but the storage space is limited. Java cannot be directly controlled through programs.

(2) STACK: It is located in RAM (Random Access Memory) and directly supported from the processor through the stack pointer. If the stack pointer moves down, a new memory is allocated. If the stack pointer moves up, the memory is released. Second only to the register allocation method.

Moving the stack pointer involves the object life cycle (the stack pointer moves, disconnects the referenced object from the stack, and the object will be recycled by the garbage collection bin ).

Normally, the reference is stored in the stack, but the Java object is not stored here, but stored in the heap.

 

In Java 8, the basic types are boolen, char, byte, short, int, long, float, and double, stored in the stack.

For example, when int a = 3 is defined, the compiler allocates a fixed area in the stack based on the int type to store the literal value 3, and creates a reference to, a is a reference pointing to the region of storage 3 (the reference is stored in the stack of course)

At this time, if int B = 3 is defined, the system first looks for the literal in the stack, and then points B's reference to 3. At this time,, B points to the same region. This is an important feature of Stack: data sharing.

If int c = 4 is defined, the compiler cannot find the literal 4 in the compiler. It will allocate space storage to the int and create a reference to c.

Note: The reference here is actually a 4-byte address stored in the stack, which stores the addresses in other regions (similar to the C ++ pointer ).

String s1; // declare s1 = new String ("abc"); // instantiate String s3; // declare s3 = new String ("abc "); // instantiate // The comparison result is true. This is the content comparison System. out. println (s1.equals (s3); // The comparison result is false, which is a reference comparison. because the reference location is different, the result must be different. System. out. println (s1 = s3 );

(4) constant storage: constant values are usually stored in program code and used to store string constants and basic type constants. They are generally defined using public static final.

(5) Non-RAM storage: it is usually worth a hard disk. Two basic examples: Stream object and Persistent Object

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.