Objects and Object Variables

an object variable doesn’t actually contain an object. It only refers to an object. And the special value null to indicate the absence of an object.

the value of any object variable is a reference to an object that is stored elsewhere. (The return value of the new operator is also a reference.)

Java object variables as analogous to object pointers in C++

C++ 做了这样的努力,让“克隆”一个对象容易,但在 Java 中必须要用 clone 方法

正是因为在 Java 中,表现的像是个指针,所以在理解

final StringBuffer s = new StringBuffer();
s.append("HelloWorld!");

时,要认为这其实是一个 const pointer,因此可以实现

Use of Multiple Source Files

我们通常会把每个类放进单独的java文件中,所以我们怎么去做多文件的编译呢?

javac Employee*.java
javac EmployeeTest.java

对于第一种方法,利用通配符 (wildcard) 可以把符合的文件都一起编译了

我们注意到第二种方法也行得通。实际上编译器在编译这个文件时,发现里头用了别的class,则会寻找这个文件

Info

you can think of the Java compiler as having the make functionality already built in.

Final Fields/Object Variable

final field 要求这个变量一定要在 construct 结束之前确定 field value

A class is immutable if none of its methods ever mutate its objects. (For example, the String class is immutable.)

Final and Mutable Object

private final StringBuilder evaluations = new StringBuilder();
public void giveGoldStar() { 
	evaluations.append(LocalDate.now() + ": Gold star!\n"); 
}

The final keyword merely means that the object reference stored in the evaluations variable will never again refer to a different StringBuilder object. But the object can be mutated.

其实也没错,因为不是说了吗,这就是一个指针,无非就是一个 const pointer 而已 (经验证,C++ 中类似的操作不行,因为 C++ variable 就是 object 自己)


System.out 就是一个例子

Private

private 是可以被类内访问,所以别的对象的方法也可以访问同类传值的私有域

Public & Class

public 修饰的 class 一定要放在自己的编译单元

如果不加,则可以呆在人家的编译单元,访问性为这个 package