什么时候用assert?

assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制。在实现中,assertion就是在程序中的一条语句,它对一个boolean表达式进行检查,一个正确程序必须保证这个boolean表达式的值为true;如果该值为false,说明程序已经处于不正确的状态下,系统将给出警告或退出。一般来说,assertion用于保证程序最基本、关键的正确性。assertion检查通常在开发和测试时开启。为了提高性能,在软件发布后,assertion检查通常是关闭的

写clone()方法时,通常都有一行代码,是什么?

clone有缺省行为,super.clone();因为首先要把父类中的成员复制到位,然后才是复制自己的成员。

下面的代码有什么不妥之处?

  1. if(username.equals("zxx")){}

username可能为null,会报空指针错误;改为”zxx”.equals(username)

去掉一个 Vector集合中重复的元素。

HashSet set = new HashSet(vector);

代码查错

abstract class Name {
  private String name;
  public abstract boolean isStupidName(String name) {}
}

错误,abstract method必须以分号结尾,且不带花括号。

public class Something {
  void doSomething() {
    private String s = "";
    int l = s.length();
  }
}

错误,局部变量前不能放置任何访问修饰符(private,public,和protected)。final可以用来修饰局部变量

abstract class Something {
  private abstract String doSomething ();
}

错误,abstract的methods不能以private修饰。abstract的methods就是让子类implement(实现)具体细节的,怎么可以用private把abstract method封锁起来呢?(同理,abstractmethod前不能加final)。

public class Something {
  public int addOne(final int x) {
    return ++x;
  }
}

错误,int x被修饰成final,意味着x不能在addOne method中被修改。

public class Something {
  public static void main(String[] args) {
    Other o = new Other();
    new Something().addOne(o);
  }
  public void addOne(final Other o) {
    o.i++;
  }
}
class Other {
  public int i;
}

正确,对象o没有变,变的是o指向的对象。

class Something {
  int i;
  public void doSomething() {
    System.out.println("i = "+ i);
  }
}

正确,输出的是”i = 0”。int i 属于 instant variable (实例变量,或叫成员变量)。instant variable有default value。int的default value 是0。

public int compareTo(Object o){
   Employee emp = (Employee) emp;
   return this.id - o.id;
}

错误,如果this.id是一个比较大的负数的话,会发生向下溢出的情况[详细][1]

class Something {
  final int i;
  public void doSomething() {
    System.out.println("i = "+ i);
  }
}

错误,final int i是个final的instant variable(实例变量,或叫成员变量)。final的instant variable 没有 default value,必须在 constructor (构造器)结束之前被赋予一个明确的值 。可 以修改为”final int i =0;”。

public class Something {
  public static void main(String[] args) {
    Something s = new Something();
    System.out.println("s.doSomething() returns " + doSomething());
  }
  public String doSomething() {
    return "Do something ...";
  }
}

错误,static method 不能直接 call non-staticmethods。可改成System.out.println("s.doSomething()returns " + s.doSomething());。同理,static method 不能访问 non-static instant variable。
此处,Something 类的文件名叫 OtherThing.java

class Something {
  private static void main(String[] something_to_do) {
    System.out.println("Dosomething ...");
  }
}

正确,从来没有人说过Java的Class名字必须和其文件名相同。但public class的名字必须和文件名相同。

interface A {
  int x = 0;
}
class B {
  int x =1;
}
class C extends B implements A {
  public void pX(){
    System.out.println(x);
  }
  public static void main(String[] args) {
    new C().pX();
  }
}

错误,在编译时会发生错误(错误描述不同的JVM有不同的信息,意思就是未明确的x调用,两个x都匹配(就象在同时import java.util和java.sql两个包时直接声明Date一样)。对于父类的变量,可以用super.x来明确,而接口的属性默认隐含为public static final.所以可以通过A.x来明确。)

interface Playable {
  void play();
}
interface Bounceable {
  void play();
}
interface Rollable extends Playable, Bounceable {
  Ball ball = new Ball("PingPang");
}
class Ball implements Rollable {
  private String name;
  public String getName() {
    return name;
  }
  public Ball(String name) {
    this.name = name;
  }
  public void play() {
    ball = new Ball("Football");
    System.out.println(ball.getName());
  }
}

错误,任何在interface里声明的interface variable(接口变量,也可称成员变量),默认为public static final。也就是说Ball ball = new Ball("PingPang");实际上是public static final Ball ball = new Ball("PingPang");。在 Ball 类的 Play()方法中,ball = new Ball("Football");改变了ball的reference,而这里的ball来自Rollable interface,Rollable interface 里的 ball 是 public static final 的,final的object是不能被改变reference的。因此编译器将在ball = newBall("Football");这里显示有错。