1、&&(短路与),&(与),|(或),||(短路或)
区别是&&只要满足第一个条件,后面条件就不再判断,而&要对所有的条件都进行判断。|和||类似。
2、super.getClass().getName()
由于getClass()在Object类中定义成了final,子类不能覆盖此方法,因此在子类中调用getClass().getName()与super.getClass().getName()都是一样的,都返回子类的名称。如果要返回父类的名称,调用getClass().getSuperClass().getName()。
3、关于String的substring(0)
查看源码:
public String substring(int start) {
if (start == 0) {
return this;
}
if (start >= 0 && start <= count) {
return new String(offset + start, count - start, value);
}
throw indexAndLength(start);
}
4、short s1 = 1; s1 = s1+ 1;错误的
s1为短整型,与1相加时向上转型为int,再赋值给短整型,需要强制转换
5、继承后的构造函数
class A{
int x=0,y=0;
A(int x,int y) {this.x = x; this.y = y;}
}
class B extends A{
int i=0,j=0;
B(int i, int j){ this.i = i; this.j = j; }
}
如果自己没有自定义构造函数,会自动创建一个默认构造函数,会在创建对象时自动调用。A、B中都没有默认构造函数,在B构造函数中,必须显示的调用super,因为只有默认构造函数才能自动调用。此例子中,如果去掉A的自定义构造函数,代码正常。