创建一个对象的过程(以对象Dog为例):
- The first time an object of type Dog is created (the constructor is actually a static method), or the first time a static method or static field of class Dog is accessed, the Java interpreter must locate Dog.class, which it does by searching through the classpath. Feedback
- As Dog.class is loaded (creating a Class object, which you’ll learn about later), all of its static initializers are run. Thus, static initialization takes place only once, as the Class object is loaded for the first time. Feedback
- When you create a new Dog( ), the construction process for a Dog object first allocates enough storage for a Dog object on the heap. Feedback
- This storage is wiped to zero, automatically setting all the primitives in that Dog object to their default values (zero for numbers and the equivalent for boolean and char) and the references to null. Feedback
- Any initializations that occur at the point of field definition are executed. Feedback
- Constructors are executed. As you shall see in Chapter 6, this might actually involve a fair amount of activity, especially when inheritance is involved. Feedback
数组:
int [] a;
int a[];都可以,但是前者在java中用得多些。
java垃圾回收机制(简要):
java中一般情况下不需要手动清除内存,除非特殊的情况(比如调用native方法用C/C++创建的对象,需要在finanlize方法中用对应的方法销毁对象)