// super(text);
I= 2;
//隐式超级构造super1()是未定义的。必须明确援引另一个构造
}
public static void main (String args[]){
sub1 sub2 = new sub1("Hello");
System.out.println(sub2.I);
}
}
八. java中的异常处理
1. java中的异常分运行时异常 和 非运行时异常, 运行时异常由运行时系统捕获并处理(编译正常),非运行时异常必须由处理(抛出或捕获)
2. 异常机制中try{}后一定要跟catch吗
* 不一定,,但必须跟finally.也就是catch和finally必须跟其中一个
* 异常机制中try{}后一定要跟catch吗
* 不一定,,但必须跟finally.也就是catch和finally必须跟其中一个
* try {
* }finally {}
* 这样没问题,而且,可不是没有意义哦,因为这样可以保证即使发生了异常,finally里面的代码一定会被执行。
* 有时候,这个还是非常有用的。
* 比如可以用来释放一些自己占用的资源,然后让调用者处理异常。
3. 异常中的finally一定会执行,哪怕一个方法中有return语句,也是在异常处理后才返回
4. 异常的抛出可以先子类再父类,如果子类捕获了,则父类就不再捕获;
但是不能先父类再子类,那样会导致编译出错
5. 异常处理后,程序继续执行
实例:
[java]
/*
* 非运行时异常一旦抛出,要么用catch块捕获处理,要么声明抛出
*/
import java.io.IOException;
public class ExceptionTest {
// public static void methodA(){
public static void methodA() throws IOException {
// throw new NullPointerException();
// try{
throw new IOException();
// System.out.println("method exit");
// }catch(IOException e){}
// finally{}
}
public static void main(String[] args) {
try {
methodA();
// throw new IOException();
} catch (IOException e) {
System.out.println("Caught1 IOException ");
} catch (NullPointerException e) {
System.out.println("Caught1 NullPointerException");
} catch (Exception e) {
System.out.println("Caught Exception");
}
System.out.println("main exit");
}
}
/*
* 非运行时异常一旦抛出,要么用catch块捕获处理,要么声明抛出
*/
import java.io.IOException;
public class ExceptionTest {
// public static void methodA(){
public static void methodA() throws IOException {
// throw new NullPointerException();
// try{
throw new IOException();
// System.out.println("method exit");
// }catch(IOException e){}
// finally{}
}
public static void main(String[] args) {
try {
methodA();
// throw new IOException();
} catch (IOException e) {
System.out.println("Caught1 IOException ");
} catch (NullPointerException e) {
System.out.println("Caught1 NullPointerException");
} catch (Exception e) {
System.out.println("Caught Exception");
}
System.out.println("main exit");
}
}
九. 按位运算和逻辑运算
按位运算操作符(& ,| )两边的都要计算
逻辑运算如果操作符(&&, || )左边成立则就不在计算右边了
实例:
[java] view plaincopyprint public class test {
private static int j = 0;
private static boolean methodB(int k) {
j += k;
return true;
}
public static void methodA(int i) {
boolean b;
b = i < 10 | methodB(4);
b = i < 10 || methodB(8);
}
public static void main(String args[]) {
methodA(0);
System.out.println(j);
}
}
public class test {
private static int j = 0;
private static boolean methodB(int k) {
j += k;
return true;
}
public static void methodA(int i) {
boolean b;
b = i < 10 | methodB(4);
b = i < 10 || methodB(8);
}
public static void main(String args[]) {
methodA(0);
System.out.println(j);
}
}What is the result
A.The program prints “0”
B.The program prints “4”
C.The program prints “8”
D.The program prints “12”
E.The code does not complete
十. for(;;)意义
相当于while(true), 不知道java为什么要搞出这个古怪让人费