Certification Key for SCJP1.4 Section 1 Declarations and Access Control Objective 1, Creating Arrays Write code that declares, constructs and initializes arrays of any base type using any of the permitted forms, both for declaration and for initialization
目标1, 创建数组 采用不同的格式来编写任一基本数据类型数组的声明,构造及初始化的代码。
数组是一连串对象或基本数据,它们必须同型,并以一个标识符封装在一起。数组好象一个对象,通过new关键字来创建。 声明数组 数组的声明并不能为数组分配内存。声明时数组不能有大小信息,也就是说编译器并不允许你告诉它究竟数组有多大,它只是一个reference(引用),并没有对应的空间。声明数组的方式有: int[] a1; int a1[]两种,int num[5]是错误的数组声明方式。
声明并创建数组 声明一个数组并同时为它分配内存。 Int num[] =new int[5];
声明并初始化数组 声明一个数组并同时进行初始化。 Int num[]=new int[]{0,1,2,3,4}; Int num[]=new int[5]{0,1,2,3,4}; //!错误
示例: public class MyAr{ public static void main(String argv[]){ int[] i = new int[5]; int i[5]; //!编译错误 int[] m[]={{1,2,3,4},{2,3,4,5},{4,5,6,7}}; int[][] n={{1,2,3,4},{2,3,4,5},{4,5,6}}; int j; m=n; for(int k=0;k<n.length;k++){ System.out.println(n[k].length); } System.out.println(i[5]); //!运行错误,超界 System.out.println(i[4]); //正确,打印0 System.out.println(j); //!编译错误,没有初始化 For(int k=0;k<i.length;k++){ I[k]=k; } } }
Objective 2, Declaring classes and variables Declare classes, inner classes, methods, instance variables static, variables and automatic (method local) variables, making appropriate use of all permitted modifiers (such as public final static abstract and so forth). State the significance of each of these modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.
Final Final修饰符可用于类,方法和变量,fianl类不能被继承,所有final类中的方法都是教学final方法。Final变量的值必须在创建时设定并具不能被修改。
Synchronized 防止多个线程同时访问相同的代码段。
Transient 表明类序列化时,变量不必序列化。
Volatile 表明变量由于线程可能异步修改。
示例: abstract class Base{ abstract public void myfunc(); //抽象方法 public void another(){ //实例方法 System.out.println("Another method"); } static void smethod(){ //类方法 System.out.println("base smethod"); } }
public class Abs extends Base{ public static void main(String argv[]){ Abs a = new Abs(); Base b=new Abs(); a.amethod(); // 动态绑定,运行时调用 a.smethod(); //静态方法,编译时调用 b.smethod(); }
public void myfunc(){ System.out.println("My func"); }
Objective 3, The default constructor For a given class, determine if a default constructor will be created and if so state the prototype of that constructor
示例: class Base{ int i; Base(int i){ this.i=i; System.out.println("single int constructor"); } void Base(){ System.out.println(“in void Base()”); } //Base(){} }
public class Cons extends Base { Cons(int i){} //Cons(int i){super(i);} public static void main(String argv[]){ Base c = new Base(); //编译错误,没有构造函数 Cons n=new Cons(3); //编译错误,Base没有无参数构造函数,void Base()函数不是构造函数。 } }
Objective 4, Overloading and overriding State the legal return types for any method given the declarations of all related methods in this or parent classes.
public void another(int i){ System.out.println("Another int method"+i); } //public int another(int i){} //!编译错误,重复定义 public void another(double d){ System.out.println("Another double method "+d); } static void smethod(){ System.out.println("base smethod"); } }
public class Abs extends Base{ public static void main(String argv[]){ Abs a = new Abs(); Base b=new Abs(); a.amethod(); a.smethod(); b.smethod(); a.another(4); a.another(4.9f); // 注意:它调用了覆写方法 b.another(4.9f); // 它不调用覆写方法 }