关于实参中的null?????

来源:百度知道 编辑:UC知道 时间:2024/05/15 16:39:20
class Gizmo {
public void spin() {}
}

public class FinalArguments {
void with(final Gizmo g) {
//! g = new Gizmo(); // Illegal -- g is final
}
void without(Gizmo g) {
g = new Gizmo(); // OK -- g not final
g.spin();
}
// void f(final int i) { i++; } // Can't change
// You can only read from a final primitive:
int g(final int i) { return i + 1; }
public static void main(String[] args) {
FinalArguments bf = new FinalArguments();
bf.without(null);
bf.with(null);
}
}
上面的两句 bf.without(null);
bf.with(null);
它们与 bf.without();
bf.with();有什么不同???
一直没见过null可在实参中出现……

null表示传了一个空值过去。
因为with的原型为with(Gizmo g),应该要传一个Gizmo对象,null表示传了一个空对象(相当于C里面的空指针)。如果只是with(),那就参数不匹配了,因为with要接收一个参数,而with()没传。without的道理也一样的