①/*声明语句也算一次执行*/ 所以下面语句会出错
if(1==1) String s= null ;
②Runtime异常不要求处理所以test()不需要throws Exception
public static void main(String[] args) {
test();
}
public static void test(){
if(1==1) {
throw new RuntimeException("test");
}
}
③子类继承了父类的变量,如果子类重新声明,则隐藏了父类所声明的变量
public class Exzample{
static String s ="S";
public static void main(String[] args) {
S2 s2 = new S2();
//s2.display(s);
s2.display();
//s2.display(s2.s);
}
}
class S1{
String s = "father";
void display(String s){
System.out.println(this.s+s);
}
void display2(){
System.out.println(s);
}
}
class S2 extends S1{
String s ="son";
// S2(){
// super();
// s="son";
// }
//String s = "son";
void display(){
System.out.println(this.s+super.s);
}
}
④根据Java的规范,两个对象equals方法为true时,一定要有相同的HashCode,但是有相同的HashCode的两个对象
不一定equals。这个其实很容易找到例子,比如String的 HashCode的计算方法就是s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
String a = "!}";
String b = "\"^";
System.out.println(a.hashCode());//1148
System.out.println(b.hashCode());//1148
System.out.println(a.equals(b));
⑤下面输出为integer!!
public class NullParameter {
public void overload(Object o){
System.out.println("object");
}
public void overload(Integer it){
System.out.println("integer");
}
public static void main(String args[]){
NullParameter n = new NullParameter();
n.overload(null);//integer
}
}