不要上机,先猜猜输出什么?public class Test3 {
String str = "";
public static void main(String[] args) {
String test = new Test3().method(1);
System.out.println(test);
}
private String method(int a){
try {
if(a == 1){
throw new Exception();
}
str +="111";
} catch (Exception e) {
str +="222";
return str;
}finally{
str += "333";
}
str += "444";
return str;
}
}
String str = "";
public static void main(String[] args) {
String test = new Test3().method(1);
System.out.println(test);
}
private String method(int a){
try {
if(a == 1){
throw new Exception();
}
str +="111";
} catch (Exception e) {
str +="222";
return str;
}finally{
str += "333";
}
str += "444";
return str;
}
}
str="444"???
所以执行
throw new Exception();
异常被捕获
str +="222";
return str;
执行return 前要先执行finally
所以执行str += "333";
结果是222333
我没有运行过哦,错了谅解
String str = "";
int i=0;
public static void main(String[] args) {
// String test = new Test01().method(1);
// System.out.println(test);
System.out.println(new Test01().i);
}
private String method(int a){
try {
if(a == 1){
throw new Exception();
}
str +="111";
} catch (Exception e) {
str =str+"222";
i++;
System.out.println(str);
return str;
}finally{
System.out.println("有没有执行");
str =str+ "333";
i++;
System.out.println(str);
System.out.println("有没有执行");
}
str += "444";
return str;
}
}
private 是可以在本类的其他方法中调用的嘛
package test;
public class Test3 {
String str = "";
public static void main(String[] args) {
String test = new Test3().method(1);
System.out.println(test);
String test1 = new Test3().method(0);
System.out.println(test1);
}
private String method(int a){
try {
if(a == 1){
throw new Exception();
}
return str +"111";
} catch (Exception e) {
str +="222";
return str + "444";
}finally{
str += "333";
return str + "555";
}
}
}
public class Test01 {
StringBuffer s=new StringBuffer("");
public static void main(String[] args) {
System.out.println(new Test01().method1(1));
}
private StringBuffer method1(int a){
try {
if(a == 1){
throw new Exception();
}
s.append("1111111111");
} catch (Exception e) {
// str ="222";
s.append("aaaa");
return s;
}finally{
s.append("333");
}
s.append("444");
return s;
}
}
经过测试,勉强可以理解了.
当然执行到return str;的时候,这时候已经复制了这个引用了.所以返回的是"222"这个对像的地址,然后str重新指向了新的对像.可以比较一下边的结果.它返回的是aaaa而不是那个99999999public class Test01 {
StringBuffer s=new StringBuffer("");
public static void main(String[] args) {
System.out.println(new Test01().method1(1));
}
private StringBuffer method1(int a){
try {
if(a == 1){
throw new Exception();
}
s.append("1111111111");
} catch (Exception e) {
// str ="222";
s.append("aaaa");
return s;
}finally{
s=new StringBuffer("99999999999999");
}
s.append("444");
return s;
}
}楼主的代码改一下应该就可以看出不同的地方:public class Test3 {
String str = "";
public static void main(String[] args) {
Test3 t = new Test3();
String test = t.method(1);
System.out.println(test);
System.out.println(t.str);//这个就应该是222333
}
private String method(int a){
try {
if(a == 1){
throw new Exception();
}
str +="111";
} catch (Exception e) {
str +="222";
return str;
}finally{
str += "333";
}
str += "444";
return str;
}
}
根据这个代码的结果发现.总是返回最后一个return 滴~~.
其实String test = new Test3().method(1); 中的test并非是Test3的对象中全局变脸str的值如果写成Test3 t = new Test3();
String test = t.method(1);
System.out.println(t.str);//这里输出的才是全局str,(*^__^*) 嘻嘻……