Table of Contents
Write in front
Card 01. Use references to manipulate objects
Card 02. How is memory allocated when the program is running?
Home Java javaTutorial 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

Aug 03, 2018 pm 02:56 PM
java Programming ideas

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;
Copy after login

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";
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles