JSCP1.4 考点总结一、二、

Certification Key for JSCP1.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}; //!错误

 

  数组知道自己的大小

 c++不同,数组知道自己的大小,当数组越界时,会抛出ArrayIndexOutOfBoundsException异常。数组具有length属性(不是length()方法),它能告诉你数组的大小。

 

 多维数据

 int m[][] ;     int [] m[];    int[][] m;

 

数组的缺省值

与其它的变量不同,不论数组在向处创建,它总是使用可以使用缺省值。

 

示例:

          public class MyAr{
  
  
                        public static void main(String argv[]){
  
  
                     int[] i = new int[5];
  
  
                                     int i[5];  //!编译错误
   
   
                                     int[] m[]={{1,2,34},{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.

目标2 声明类与变量

声明类,内部类,方法,实例变量,静态变量,自动变量(局部变量),正确使用各种修饰符(public , private , static ,final, abstract)

 

JAVA中万事万物皆对象,即使JAVA程序本身也是对象。

 类的定义和对象的生成

  public class MyClass{    //类定义

int i;

float f;      //类数据成员

void amethod(){  //方法

int i;     // 局部变量

}

}

 

MyClass aClass =new MyClass();   //创建类的一个实例(对象)

 

修饰符说明

 private

被了变量所在的类,其它任何类都不可以访问这个成员。

 

无访问修饰符

所谓的包访问权限,同一个包内的其它类可以访问这个成员。

 

Protected

与无访问修饰符具有权限外,还允许子类访问这个成员。

 

Public

具有全局可视性,任何其它类都可以访问这个成员。

 

Static

Static变量称为类变量,类的所有对象共享这个变量。

Static方法称为类方法,它只能访问static变量。静态方法不能被子类overriding为非静态方法,但静态方法可以被子类静态方法所Hided.

 

Native

只用于方法,指明方法体是由其它编程语言编写的。它以;结尾不是以{}结尾。

Public native void fastcalc();

 

Abstract

Abstract修饰符可用于类与方法前,在用于方法前时表明方法不具备方法体。只支类中包括了抽象方法则它必须声明为抽象类。如果一个类实现一个接口时,它没有为接口中所有的方法提供实现时,也必须声明为抽象类。

 

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");

       } 

      

        public void amethod(){  //实现抽象方法

                myfunc();

        }

       

        static void smethod(){  //hiding 父类静态方法

                System.out.println("Abs smethod");

        }

}

 

 

 

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

目标3 缺省构造器

结定一个类,确定是否有缺省构造器

 

构造器是与类名相同的方法,并具没有返回值。缺省构造器是一个不具有任何参数的构造器。在你没有提供构造器的条件下,编译器为自动化为你提供一个缺省的构造器,但一旦你定义了任何一个构造器,编译器就不会为你提供缺省构造器。在这种条件下,如果你使用不具有参数的构造器将有错误。

构造器可以有访问修饰符,但不能有native,abstract,static,synchronizedfinal修饰符。

 

示例:

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.

目标4 重载与覆写

为所有在自己或父类中的相关方法声明有效的返回值,

相同类中的方法

当在同一个类中有多个方法具有相同的名称时,这个方法就被重载了。只有参数的次序和类型是区分重载方法的依据,而返回值和参数的名称对区分重载方法没有贡献,所以,不能以返回值的不同来重载方法。

 

子类中的方法

           可以在子类中重载父类的方法,只要新的重载方法具有不同的参数次序或类型。当你在子类中的方法具有于父类中的方法相同的signature,则称子类覆写了父类的方法。注意:子类覆写父类方法,它的访问修饰符可以不同,但子类要具有比父类更加严格的访问权限。

静态方法不能被覆写只能被HIDED

 

基本类型为参数的重载

           基本类型可以自动进行窄化转型(narrowing conversion),在没有相应数据类型的重载方法,它的数据类型向上晋升。

 

示例:

class Base{

          

        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);  // 它不调用覆写方法

        }

 

        public void myfunc(){

                System.out.println("My func");

       } 

      

        public void amethod(){

                   myfunc();

        }

       

        static void smethod(){

                   System.out.println("Abs smethod");

        }

       

        void another(float f){

                   System.out.println("Abs another float method"+f);

        }

       

//        void another(int i){  //编译错误,访问权限弱化

//                 System.out.println("Abs another method"+i);

//        }

       

        public void another(int i){

                   System.out.println("Abs another int method"+i);

        }

}

 

 

Section 2 Flow control and exception Handling

Objective 1, The if and switch statements

Write code using if and switch statements and identify legal argument types for these statements.

目标1 if switch语句

使用ifswitch编写代码并能区分有效的参数类型

 

if的条件语句中只能是boolean型的数据。

Int k=1;

If(k )  System.out.println(k);  // 错误

If(k==1) System.out.println(k);  // 正确

 

Switch的条件语句中只能是int数据类型,或int的窄化数据类型也就是byte, char, short

Long i;

Switch(i){  //错误

        Case 1: .. break;

  Case 2: … break;

  Default: ..

}

case语句的break的使用,在switch块中只有遇到break语句时,块才跳出。

Default语句不必总安排在switch语句的最后,但default语句不在最后时,要使用break.

 

三元操作符 ?:

 

 

示例: 

public class MySwitch{

        public static void main(String argv[]){

                  MySwitch ms= new MySwitch();

                  ms.amethod();

                  ms.ifmethod();

       }

 

 

        public void amethod(){

               byte k=30;  // byte,char,short,int

                switch(k){

                            default: //Put the default at the bottom, not here

                                System.out.println("This is the default output");

                             case 10:

                                System.out.println("ten"); break;

                             case 20:

                                System.out.println("twenty");  break;

               }

        }

       

        public void ifmethod(){

                    int k=1;

                    boolean b;

                    //if(k=1) System.out.println("One");

                    if(k==1) System.out.println("one");

                    if(b=true) System.out.println("true");

        }

}

输出结果:try it

 

Objective 2, looping, break and continue

Write code using all forms of loops including labeled and unlabeled use of break and continue and state the values taken by loop counter variables during and after loop execution.

目标2 循环,breakcontinue

   编写各种(不)具有labelbreakcontinue的循环语句。明确循环开始与结束时,循环记数器的值。

 

For 循环

语法:

        for(initialization; conditional expression;increment)

 

逗号运算符

           它只能用于for的控制表达式中。

for(int i=1,j=i+10;i<5;i++,j=i*2)

do-whilewhile的区别在开do-while中的语句至少执行一次。

 

Gotobreak,continue

  虽然gotojava的保留字,但java而不使用它。Java中的跳跃功能由breakcontinue提供。在java中,惟一一个放置lable而能够产生效益的地点就是恰恰放在迭代语句前。一般而论,breakcontinue只会中断当前的循环,而与label搭配,可以中断多层循环。

Label1:

 Outeriteration{

      Inneriteration{

                

                break;  //1

       

        continue; //2

                ….

                Continue label1;//3

                 ….

                 Break label1; //4

}

}

1中断内层迭代,回到外层迭代

2将执行点移至内层迭代的起始处

3同时中断内层迭代,再从外层迭代开始

4同时中断内外层迭代,不再进行任何迭代

务必记下。在java中使用label的惟一理由是跳过一个以上的嵌套层次。

示例:

public class LabeledWhile {

  public static void main(String[] args) {

    int i = 0;

    outer:

    while(true) {

      System.out.println("Outer while loop");

      while(true) {

        i++;

        System.out.println("i = " + i);

        if(i == 1) {

          System.out.println("continue");

          continue;

        }

        if(i == 3) {

          System.out.println("continue outer");

          continue outer;

        }

        if(i == 5) {

          System.out.println("break");

          break;

        }

        if(i == 7) {

          System.out.println("break outer");

          break outer;

        }

      }

    }

  

  }

}

输出结果: Just try it

 

Objective 3, try/catch and overridden methods

Write code that makes proper use of exceptions and exception handling clauses (try catch finally) and declares methods and overriding methods that throw exceptions.

目标3 try/catch语句与方法覆写

           正确使用异常与异常处理语句(try,catch,finally)来编写代码,正确处理覆写方法的异常。

 

异常一词,意指“我对这件事有异议:我对这件事感到意外”。在问题发生点,你可能自己处理异常,有时,你可能不知道如向处理,就是说在当前的context并不具备中以修改问题的信息,你可将问题交付给高层的context的处理。

 

覆写有异常抛出的方法

           覆写具的异常抛出的方法时,子类的方法只能抛出父类方法所抛出的异常或它的子异常。但是,子类方法可以抛出少于父类的异常或干脆就不抛异常。

方法声明的Throws子句

           抛出子句意指当错误发生时,方法抛出这个异常而这个方法的调用者必须处理(catch)这个异常异常。

 

示例:

import java.io.*;

class Base{

           public static void amethod()throws FileNotFoundException{}

           public FileInputStream openFile(String filename)

throws FileNotFoundException{//抛出异常,自己不处理

                      FileInputStream fis = new FileInputStream(filename);

                      return fis;

           }

}

 

 

 

public class ExcepDemo extends Base{

           public static void main(String argv[]){

                  ExcepDemo e = new ExcepDemo();

                  try{  //调用者要处理抛出的异常

                  e.openFile("xx");

                  }catch(FileNotFoundException ex){

                               ex.printStackTrace();

                  }

           }

 

           public static void amethod(int i)throws IOException{} //overload

           //public static void amethod() throws IOException{} //!编译错误 override

           private boolean ExcepDemo(){  //不是构造方法

            try{

               DataInputStream din = new DataInputStream(System.in);

               System.out.println("Pausing");

               din.readChar();

               System.out.println("Continuing");

               this.amethod();

               return true;

               }catch(IOException ioe) {}

               finally{

               System.out.println("finally");

               }

               return false;

             }

 

}

 

Objective 4, When Exceptions occur

Recognize the effect of an exception arising at a specified point in a code fragment. Note: The exception may be a runtime exception, a checked exception, or an error (the code may include try, catch, or finally clauses in any legitimate combination).

 

目标4 如时异常发生

           认识代码段中异常发生的地方以它的影响。注意:异常可能是运行时异常,检测异常或错误。

 

错误无需捕捉

异常的出处:

1.             Java标准库类抛出的异常

2.             自己的类抛出的异常

3.             执行期发生的任何意外

检测异常与非检测异常

检测异常你必须捕捉,而非检测异常你不必捕捉。

非检测异常发生后,缺省条件下会在控制台上打印一条消息。对某些非检测异常可用编码来避免。

public class GetArg{
  
  
    public static void main(String argv[]){
  
  
        if(argv.length ==0){
  
  
                     System.out.println("Usage: GetArg param");
  
  
        }else{
  
  
                     System.out.println(argv[0]);
  
  
        }
  
  
    }
  
  
}
  
  
检测异常必须被捕捉,方法在调用某个抛出检测异常的的方法时,它或者捕捉它或抛出它。
   
   

public FileInputStream openFile(String filename)

throws FileNotFoundException{//抛出异常,自己不处理

                      FileInputStream fis = new FileInputStream(filename);

                      return fis;

           }

public FileInputStream openFile(String filename) {

           FileInputStream fis;

try{

fs= new FileInputStream(filename);

}catch(FileNotFoundException ex){

      ex.printStackTrace();

}

                      return fis;

           }

 

finally子句

 finally子句总是会执行,即使try/catch中已有了return语句finally中的语句也会执行。但如果try/catch中有System.exit(0)finally语句不会执行。
   
   

  
  
   
    
  
  

  
  
   
    
  
  
Catch子句
    
    
 Catch子句的捕捉异常的次序要与异常的层次结构相一致。也就是说子异常要先捕捉,父异常后捕捉。反之,编译器会抛出子异常已捕捉的信息。其它要注意是:异常被抛出后,异常处理根据捕捉的排列次序,寻找最近的处理程序。
   
   

  
  
   
    
  
  
示例:
   
   
class ThreeException extends Exception {}
   
   
class subException extends ThreeException{}
   
   

  
  
   
    
  
  
public class FinallyWorks {
   
   
  static int count = 0;
   
   
  public static void main(String[] args) {
   
   
    while(true) {
   
   
      try {
   
   
        // Post-increment is zero first time:
   
   
        if(count++ == 0)
   
   
          throw new subException();
   
   
        System.out.println("No exception");
   
   
      /*} catch(subException e) {                //catch的次序要如此,反之出错
   
   
        System.err.println("ThreeException");*/
   
   
      }catch(ThreeException e){
   
   
         System.out.println(“subException”);
   
   
       } finally {
   
   
        System.err.println("In finally clause");
   
   
        if(count == 2) break; // out of "while"
   
   
      }
   
   
    }
   
   
   
   
   
  }
   
   
}
   
   

 

Objective 5 and 6 Using Assertions

Write code that makes proper use of assertions, and distinguish appropriate from inappropriate uses of assertions Identify correct statements about the assertion mechanism.

目标5 使用断言

正确编写断言代码

assertion功能提供了一种在代码中进行正确性检查的机制,这种检查通常用于开发和调试阶段,到了软件完成部署后就可以关闭。这使得程序员可以在代码中加入调试检查语句,同时又可以在软件部署后关闭该功能而避免对软件速度和内存消耗的影响。基本上,assertion功能就是JAVA中的一种新的错误检查机制,只不过这项功能可以根据需要关闭。

 

断言的语法

assert somebooleatest

assert somebooleantest : someinformatinvemethod

 

断言的使用

断言的使用就好比注释的使用,注释向其它人表明其阅读的代码是正确的,而断言用于保证在程序执行过程中booleanTest的值一定是真。断言用于确保某些东西总是为真的。缺省条件下,断言功能是关闭的。
   
   
编译开启断言功能。
   
   
javac -source1.4 Myprog.java
   
   
运行开启断言功能
   
   
enableassertions的参数:
no arguments 
   Enables or disables assertions in all classes except system classes. 
packageName... 
   Enables or disables assertions in the named package and any subpackages. 
...
   Enables or disables assertions in the unnamed package in the current working directory. 
className
   Enables or disables assertions in the named class
例如:
java –enableassertions:my.package…  MyProg  
     
     
java –ea Myprog

  
  
   
    
  
  

  
  
   
    
  
  

  
  
   
    
  
  

  
  
   
    
  
  
What should you assert to be true?
断言用于你认为某此东西必须是真的地方。例如:人的年龄必然大于0,又如你认为在一套if/else判定后,必然有判定成功的分支,就可在if/else后,插入断言。例如:
        switch(lang){
        case Language.java:
            System.out.println("java");
            break;
        case Language.pascal:
            System.out.println("pascal");
            break;
        case Language.csharp:
            System.out.println("csharp");
            break;
        default:
            assert false : lang;
        }

  
  
   
    
  
  

断言使用的地方

应该使用的情形

不应该使用的情形

用于保证内部数据结构的正确

不用于保证命令行参数的正确

用于保证私有(private)方法参数的正确

不用于保证公共(public)方法参数的正确

用于检查任何方法结束时状态的正确

不用于检查外界对公共方法的使用是否正确

用于检查决不应该出现的状态

不用于保证应该由用户提供的信息的正确性

用于检查决不应该出现的状态,即使你肯定它不会发生

不要用于代替if语句

用于在任何方法的开始检查相关的初始状态

不要用做外部控制条件

用于检查一个长循环执行过程中的的某些状态

不要用于检查编译器、操作系统、硬件的正确性,除非在调试过程中你有充分的理由相信它们有错误

 

示例:

class Language{
    public static final int java=1;
    public static final int pascal=2;
    public static final int csharp=3;

  
  
   
    
  
  
}
public class Mgos  {
static int lang=0;
static int age;
public static void setAge(int a){
    assert age>0: “age must greater than zero”;
    age=a;
}
public static void main(String argv[]){
    setAge(-1);
        switch(lang){
        case Language.java:
            System.out.println("java");
            break;
        case Language.pascal:
            System.out.println("pascal");
            break;
        case Language.csharp:
            System.out.println("csharp");
            break;
        default:
            assert false : lang;
        }

  
  
   
    
  
  
    }

  
  
   
    
  
  
}
在不开启断言功能时的输出:
在开启断言功能时的输出:

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值