面试题一:
package com.ws;
public class TestSwitch {
public static int getValue(int i) {
int result = 0;
switch (i) {
case 1:
result = result + i;
case 2:
result = result + i * 2;
case 3:
result = result + i * 3;
}
return result;
}
public static void main(String[] args) {
int value = getValue(2);
System.out.println(value);
}
}
这部分运行的结果是:10
原理在于当i是2的时候 result=4,因为一直在运行,从上到下执行,result=4+2*3=10
面试题2:
下列哪种异常是检查型异常,需要在编写程序时声明(C)
A NullPointerException B ClassCasetException
C FileNotFoundException D IndexOutOfBoundsException
java中异常分为检查性异常和非检查性异常;检查性异常就是该类的异常难以避免,需要程序员解决这种异常,所以必须要通过try catch进行处理这些异常,比如FileNotFoundException
非检查性异常:就是编译器不检查这类异常,不检查的开发者在编译阶段就不必必须处理,比如数组越界、访问null对象,这种错误你自己是可以避免的。编译器不会强制你检查这种异常。