Preface#
"On Java" is a book worth reading. Even though I have a foundation in Java, I can still gain new understanding in the basic parts while reading this book. As the author of "Thinking in Java," the author is good at not only explaining how to use it, but also popularizing the origin of this knowledge point, why it should be used, where its value lies, and what it actually is. I don't want to forget after reading it once, so I write down my thoughts and experiences for each chapter, so that I can review them directly by reading this article in the future. Did you forget about some adolescent plan?
Chapter 1 What is an Object#
This chapter mainly summarizes the core features of Java for readers. Readers who have not learned Java can have a general understanding of Java; readers with a foundation can deepen their impression and gain the author's unique insights into Java.
The Turing official guide for this chapter is "understand it." Personally, the main knowledge points of this chapter are as follows:
The Journey of Abstraction#
All programming languages are a form of abstraction. It can even be said that the complexity of the problems we can solve directly depends on the type and quality of abstraction.
Assembly language is an abstraction of machine code; the C language of procedural programming is an abstraction of assembly language. During the era of procedural programming, programmers had to put themselves more at the level of the computer and solve problems from the perspective of the computer. This type of abstraction still requires frequent consideration of the structure of the computer rather than the structure of the problem itself.
The emergence of object-oriented programming solved the pain points of procedural programming. According to this book, early programmers needed to establish a connection between the "machine model" (solution space, which is the computer) and the "problem model" (the space where the problem actually exists, which is the business). However, the level of abstraction established by procedural programming is not high, and programmers still need to consider the computer and spend energy maintaining this connection. With object-oriented programming, the elements of the machine model and the elements of the problem model are mapped one-to-one through objects, allowing programmers to directly describe the problem model in the machine model using the powerful abstraction ability of object-oriented thinking, and solve the connection problem. What you are reading is both the solution provided by the machine model - the code, and the description of the actual business in the problem model. Obviously, this type of abstraction is more general and efficient.
The Purpose of Object-Oriented Programming#
The purpose of object-oriented programming is to create or use a certain "service" provided by objects to solve problems. Programs also provide services to users, and they achieve this by using the services provided by certain objects.
Implementation Hiding#
One of the techniques for designing a good class is to hide details. Do not expose every detail to the user, because you don't know how the user will use your class, which will create hidden dangers for modifying the class in the future. Therefore, we only provide "services" to the outside world and hide all other unnecessary information. This allows users to trust our services and not be affected by updates, and we can confidently modify and refactor without worrying about whether a piece of code may be used.
Reusability, Inheritance, and Polymorphism#
This section introduces some important features of Java. The purpose of inheritance and polymorphism is to reuse code as much as possible. For example, when we need to create something similar to an existing class, we don't need to write a bunch of code, we can just inherit it. However, the use of inheritance should be considered carefully. The principle of inheritance is to try to satisfy the "substitution principle", which means "A is B". A clearer judgment of whether to use inheritance is: "Do I need to upcast?". In actual coding, it is often necessary to make methods independent of specific classes. The method only needs to ensure that the base class is passed in, without caring about the specific subclass, eliminating the need for lengthy judgment code. This is polymorphism.
Chapter 2 Installing Java and Examples in This Book#
It is worth mentioning that this chapter does not explain environment variables and
classpath
. The installation steps are solved using package management tools such as Chocolatey for Windows and HomeBrew for Mac OS, both of which will automatically handle environment variables. Of course, we need to know that setting environment variables is for the convenience of compiling and running Java programs in the console. It's just that modern IDEs and scripts are becoming more intelligent and can automatically set environment variables.
Chapter 3 Objects Are Everywhere#
As the saying goes, "everything is an object." In this chapter, the author will discuss the most important objects in Java and the various "forces" involved in running Java programs. The Turing guide suggests "understanding the knowledge required to run and create a simple Java program." Therefore, this chapter summarizes the following key points:
Objects and References#
In this section, the author mentions that although objects are everywhere in Java, we are not actually dealing with objects themselves. We all know that programming languages ultimately manipulate data in memory, so how to safely manipulate memory is a problem. Is it better to directly manipulate memory or indirectly manipulate memory using a certain method? Java gives its ideal answer to this question: use references. References are like remote controls that control objects, call object behaviors, and change object states.
Where is Data Stored?#
This section introduces the data storage methods in Java:
-
- Registers. As we have learned in computer organization, registers are the fastest data storage method in terms of access speed, after all, they are directly stored in the central processing unit (CPU). However, registers are scarce resources, so Java does not allow manual control of register allocation.
-
- Stack. In RAM, the processor can manipulate data through the stack pointer. However, according to data structures, we know that the stack data structure is not flexible enough in terms of adding and deleting, so only some data (such as object references) are stored on the stack, and the objects themselves are not.
-
- Heap. In RAM, the heap is a general memory pool for storing all Java objects. The compiler does not care about the lifecycle of objects on the heap because there are corresponding allocation and cleanup methods, and with the progress of the times, Java's heap memory allocation mechanism has become very efficient.
-
- Constant storage. Constants are directly stored in the program code or ROM. For example, all strings and string constants are stored in the string resource pool.
-
- Non-RAM storage. In the usual sense, it refers to data that is not in memory, such as serialized objects, which are objects that can be sent to other machines, and persistent objects, which are objects saved to disk. These data storage types can save objects in other media and convert them back to objects when needed. The most typical example is database storage.
Chapter 4 & Chapter 5 Operators & Control Flow#
These two chapters elaborate on the operator and control flow syntax of Java. The Turing guide suggests "mastering both, no difficulties." Since it is basically the same as C, I will only record the points of interest after reading the book.
Be Cautious with Object Assignment#
Earlier, it was mentioned that we are not actually manipulating the objects themselves, but rather manipulating references to objects. So when performing object assignment, the reference is actually copied to another reference, which will cause both references to point to the same object, and the reference corresponding to the assigned object will no longer exist. This example tells us to be cautious with object assignment, as unexpected results may occur.
Java == C++ -- --#
This is the view of Java's creator, Commander Gosling. He believes that Java is C++ without some unnecessary and difficult content, and is a more streamlined language. Of course, it's not pure subtraction 2333
== and !=#
Encapsulated classes with the same content created by different methods will be stored in different locations in memory. The ==
and !=
operators compare object references, so when comparing objects, it is best to use equals()
.
The Ternary Operator boolean-exp ? value0 : value1
#
It is mainly used to choose one of two values to assign to a variable. It is more concise than using if-else
to achieve the same scenario.
for-in
#
float[] f = new float[10];
for(int i=0; i<10; i++)
f[i] = rand.nextFloat();
for(float x : f)
System.out.println(x);
Unfortunately, for-in
can only be used for traversal. You cannot modify the elements in f
.
for(;;)
#
The compiler treats
while(true)
andfor(;;)
equally, and the use of these two methods in Java source code is almost evenly distributed.
The Range of Math.random()
#
In fact, it was originally an example of using the switch
statement with strings. It tells us that the strict range of Math.random()
is [0,1)
.