Home  >  Article  >  Java  >  Java programming ideas: Using references to operate objects and how memory is allocated when the program is running

Java programming ideas: Using references to operate objects and how memory is allocated when the program is running

php是最好的语言
php是最好的语言Original
2018-08-03 14:56:091319browse

Write in front

First of all, what I want to say isJava Programming Thoughts - Knowledge Card will be a series of articles. The content of the article is my reading of " My experience in the book "Java Programming Thoughts", so why should I write this series of articles? There are several reasons:

  • I picked up Java again. Due to the particularity of my work projects, I haven’t used the Java language for more than a year.

  • I want to use my spare time to write something to help those in need.

  • Fragment the knowledge of the entire book and make full use of your fragmented time.

Under normal circumstances, the latest articles will be updated on my WeChat public account: Java Programming Community. If you are interested, you can follow it.
Okay, let’s get to the point:

Card 01. Use references to manipulate objects

Everyone who studies Java knows that Java is an object-oriented language (OOP). Although Java is based on C, but in comparison, Java is a more "pure" object-oriented programming language. As the saying goes "everything is an object", the Java language assumes that we only do object-oriented programming, that is to say, Before we start using Java for design, we must change our thinking and shift our attention to objects . This is the basic skill for us to use Java language.

Every programming language has its own way of processing data. Sometimes, programmers must always pay attention to what type they are dealing with. Should they manipulate elements directly, or use some indirect representation based on special syntax to manipulate objects? In C and C we use pointers. In Java this is all simplified because "everything is an object" and everything is considered an object. Although everything is treated as an object, the manipulated identifier is actually a reference to the object. Here is a real life example to help you understand. In life, we use the remote control (reference) to control the TV (object). When you want to change the channel or volume of the TV, the remote control (reference) is actually manipulated, and the remote control controls the TV (object). If you want to move around the room while still being able to operate the TV, then just carry the remote control (reference) instead of the TV (object) .
Let’s create a String reference:

String str;

The str here is just a reference, not an object. The reference can exist independently and does not necessarily need an object to be associated with it, Just like the remote control can exist independently even without a TV. But the above creation is not safe. The safe way is to initialize it when we create the reference. For example:

String str = "hello";

Card 02. How is memory allocated when the program is running?

When the program is running, there are five different places where data can be stored:

  • 1, Register : This is fastest The storage area is because it is located in a place that is different from other storage areas - inside the processor . However, the number of registers is extremely limited, so registers are allocated according to demand. You cannot directly control it, nor can you feel any signs of the existence of registers in the program.

  • 2, Stack: Located in general-purpose RAM (random access memory), but has direct support from the processor through the stack pointer . If the stack pointer moves downward, new memory is allocated; if it moves upward, that memory is released. This is a fast and efficient way of allocating storage, second only to registers. When creating a program, the Java system must know the exact lifetime of all items stored within the stack in order to move the stack pointer up and down. This constraint limits the flexibility of the program, so although some Java data is stored on the stack - especially object references, Java objects are not stored in it.

  • 3. Heap: A general memory pool (also located in the RAM area) used to store all Java objects. The advantage of the heap being different from the stack is: The compiler does not need to know how long the stored data survives in the heap. Therefore, there is a lot of flexibility in heap allocation. When you need an object, you only need to write a simple line of code using new. When this line of code is executed, storage will be automatically allocated in the heap. Of course, there is a price to pay for this flexibility: storage allocation and cleanup with the heap may take more time than storage allocation with the stack

  • 4, Constant storage:Constant values ​​are usually stored directly inside the program code, which is safe because they will never be changed. Sometimes in embedded systems, constants are isolated from other parts, so, in this case, you can choose to store them in ROM (read-only memory).

  • 5, Non-RAM storage: If the data completely survives outside the program, then he can not be subject to any restrictions by the program, can exist even when the program is not running. Two basic examples are: Stream objects and persistent objects. In "stream objects", objects are converted into byte streams, which are usually sent to another machine. In "persistent objects" , objects are stored on disk so they retain their state even if the program terminates. The trick to this storage method is to convert the object into something that can be stored on other media, and then restored to a regular, RAM-based object when needed. Java provides support for lightweight persistence, while mechanisms such as JDBC and hibernate provide more sophisticated support for storing and reading object information in the database.

Related articles:

Summary of Java programming ideas

Object-facing with Java Understanding objects, references and inner classes in programming

The above is the detailed content of Java programming ideas: Using references to operate objects and how memory is allocated when the program is running. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn