Initialization Cleanup
1.构造器名字沿用C++的解决方案:与类名相同(大小写也需要一致)
2.当new出一个对象,堆里分配内存,构造器就立马被调用,以确保正确的初始化
3.没有参数的构造器称为默认构造器,另外,构造器是没有返回值的
package welcome; public class Exercise02 { String s = "abcf"; Exercise02(){ String s1 = "abcd"; System.out.println("Constructor"); } public static void main(String[] args) { new Exercise02(); //output:Constructor Exercise02 s2 = new Exercise02(); // System.out.println(s2.s + s2.s1); s2.s1 can not be called,is not a field } }
4.overload(重载)指的是方法名(或构造器名)相同,参数不同的两个方法(或构造器)实现,注意参数顺序也可以导致重载,最好别用这种方式。
5.如果你的类没有创建构造器,那么编译器会自动为你创建一个默认构造器。
6.当两个对象调用同一个方法时,假设a,b对象调用Banana类的的peel,那么a.peel(),相当于Banana.peel(a);当然这是个内部表示,不能通过编译。
7.this的使用,如果位于一个方法内,而你想要获得目前对象的引用,可以使用this,限制是this不能用在static方法内。注意,如果你调用的是类里面的另一个方法,不需要使用this。较常用于return语句以及方法间传递对象。
public class Leaf { int i = 0; Leaf increment() { i++; return this; } void print() { System.out.println("i = " + i); } public static void main(String[] args) { Leaf x = new Leaf(); x.increment().increment().increment().print(); } } /* Output: i = 3
8.this也可以用在多构造器里,在一个构造器里调用另一个构造器,this(arguments list),但是注意,只能在一个构造器里只能调用一个构造器,还有在非构造器方法里不可这么用。还可以调用类数据成员,用法为this.variable。
package initialization; public class Exercise09 { int x; Exercise09(int i){ System.out.println("int"); } Exercise09(String s,int p){ this(1); //call another constructor this.x = p; //call int field System.out.println("String" + x); } public static void main(String[] args) { new Exercise09("abc",1); } }
9.理解static,按照定义,static方法相当于全局方法,不需要new对象,利用引用访问,所以static自然也就没有this。
10.垃圾回收器只能处理new出来的对象,手动清除垃圾需要用到finalize()方法。注意一点,垃圾回收器不是垃圾摧毁器,垃圾回收器只回收内存。JVM中采用的一种stop-and-copy垃圾处理方法,顾名思义,先将程序停止,接着将所有还有用的对象转移到另一个堆里,当然,转移之后就需要重新绑定引用和对象,而且注意,这不是后台程序,全是前台操作。导致两个问题:一是方法本身的问题,实际需要处理两个内存堆。二是就算你的程序稳定了,收集器依然会复制内存。所以后来JVM采用了mark-and-sweep方法,最先的步骤还是一样的,寻找所有可用的对象,接下来对这些对象做标记,不马上进行回收,只有当所有标记完成了,回收机制才会启动。同样的,这个方法还是需要前台操作,需要停止程序。JVM动态监视两种方法的效率,并进行动态切换。
11.关于初始化优先级,在一个类里,优先级最高的是变量(或对象),接下来才是构造器,方法。
12.static不能用于局部变量,只能用于类的变量域里,对以上的优先级来说,static变量(对象)又优先于普通变量创建,注意static变量只有在被调用或者创建static对象时才会被初始化。
package welcome; public class Exercise14 { static String s; static String s1; static { s = "abc"; s1 = "ccc"; } static void show() { System.out.println(s + s1); } public static void main(String[] args) { new Exercise14(); //nothing new Exercise14().show(); } }
13.array的初始化,注意,只是得到一个引用,如果你将array简单的重复指向另一个变量,那么这将是个浅拷贝,两个引用指向的是同一个array。
int[] a = {1,2,3,4,5}; int[] a = new int[rand.nextInt(20); Arrays.toString(a); //produces a printable version Integer[] a = new Integer[rand.nextInt(20)] Integer[] a = { new Integer(1), new Integer(2), 3 //autoboxing };
package welcome; public class Exercise16 { public static void main(String[] args) { String[] s = {"a","b","c",}; for (String x : s) { System.out.println(x); }} }
Exercise17
package welcome; public class Exercise17 { Exercise17(String s){ System.out.println("Constructor" + s); } public static void main(String[] args) { Exercise17[] x = {new Exercise17("a"), new Exercise17("b"), new Exercise17("c"),}; /*output:constructora constructorb constructorc*/ } }
14.对于参数列表的override,必须使用不同的非参数列表参数来区分调用。
//: initialization/OverloadingVarargs3.java public class OverloadingVarargs3 { static void f(float i, Character... args) { System.out.println("first"); } static void f(char c, Character... args) { System.out.println("second"); } public static void main(String[] args) { f(1, ‘a’); f(‘a’, ‘b’); } } /* Output: first second *///:~
Exercise19
package welcome; public class Exercise19 { static void f(String... args) { for (String x: args) { System.out.println(x); } } public static void main(String[] args) { f("a","v","c"); //accept commma-seperated list f(new String[] {"c","d","f"}); //accept list object }}
Exercise21
package welcome; public class Exercise21 { Spiciness degree; public Exercise21(Spiciness degree) { this.degree = degree; } public void describe() { //enum used in switch switch(degree) { case NOT: System.out.println("not spicy"); break; case MILD: System.out.println("a little bit"); break; case HOT: System.out.println("hot"); break; } } public static void main(String[] args) { for (Spiciness i : Spiciness.values()) { //check through System.out.println(i + ":" + i.ordinal()); //display order } Exercise21 plain = new Exercise21(Spiciness.NOT); plain.describe(); } }