出于学习目的,我正在玩弄例外,我有一个问题。请看下面的代码:
public static void main(String[] args) {
System.out.print(getNumber());
}
public static double getNumber() {
double number =10;
try {
String [] students = {"nick","george"};
System.out.println(students[5]);
}
catch (ArrayIndexOutOfBoundsException ex ) {
System.out.println("In catch block "+ number);
return number;
}
finally {
number +=10;
System.out.println("The value is now "+ number);
}
System.out.println("hiiiii");
return number;
}
为什么在这种情况下 main 方法的输出将是 10,为什么如果我运行相同的示例并返回一个对象,值会改变?
public static void main(String[] args) {
System.out.println("In main is "+getStringBuffer());
}
public static StringBuffer getStringBuffer() {
StringBuffer number = new StringBuffer("10");
try {
String [] names= {"george" ,"nick"};
System.out.println(names[5]);
}
catch (ArrayIndexOutOfBoundsException ex1) {
System.out.println("In catch block "+ number);
return number;
}
finally {
number.reverse();
System.out.println("In finally is "+ number);
}
System.out.println("hiiii");
return number;
}