您的位置:首页 > 编程语言 > Java开发

java中 try return finally return(转)

2016-01-09 01:00 645 查看
finally块里面的代码一般都是会执行的,除非执行 System.exit(int),停止虚拟机,断电。

1.若try代码块里面有return ,假设要return 的值 是A,A为基本类型或者被final修饰的不可变类型(如:基本类型的包装类型Integer,Double,String ),并且finally语句块里面 对try语句块里面要return 的值A做了修改 比如A+1,但是最终返回的值是不受finally里面对A值操作的影响的。看下面的示例:

1.1范围值为基本类型



  public static void main(String[] args) {
System.out.println(test1());
}

// 基本类型 try 中有return ,finally 中改变值 ,最终返回值 不受finally里代码影响
public static int test1(){
int a =20;
try {
System.out.println("test1 try block");
return a+25;
} catch (Exception e) {
System.out.println("test1 catch exception");
} finally {
a = a+10;
}
return a;
}




控制台打印输出: test1 try block 45

1.2 返回值为基本类型的包装类型(final)



public static void main(String[] args) {
System.out.println(test3());
}

//final类型(不可变)此时是基本类型的包装类型  try 中有return ,finally 中改变值 ,最终返回值 不受finally里代码影响
public static Integer test3(){
Integer a =20;
try {
System.out.println("test3 try block");
return a+25;
} catch (Exception e) {
System.out.println("test3 catch exception");
} finally {
a = a+10;
}
return a;
}




控制台打印输出:

test3 try block 45

1.3 返回值为String类型(final)



public static void main(String[] args) {
System.out.println(test4());
}

//final类型(不可变)此时是String字符串类型  try 中有return ,finally 中改变值 ,最终返回值 不受finally里代码影响
public static String  test4(){
String a ="中国";
try {
System.out.println("test4 try block");
return a+"_强大繁荣";
} catch (Exception e) {
System.out.println("test4 catch exception");
} finally {
a = a+"== 强盛!";
}
return a;
}




控制台打印输出:

test4 try block 中国_强大繁荣

2.若try代码块里面有return ,假设要return 的值 是B,B为基本类型或者被final修饰的不可变类型(如:基本类型的包装类型Integer,Double,String ),并且finally语句块里面 对try语句块里面要return 的值A做了修改 比如A+1,并且返回了值A,但是最终返回的值是受finally块里面对A值的操作的影响。看下面的示例:

2.1 返回值为基本类型



public static void main(String[] args) {
System.out.println(test2());
}

// 基本类型 try 中有return ,finally 中改变值 并return改变后的值 ,最终返回值 受finally里代码影响
public static int test2(){
int a =20;
try {
System.out.println("test2 try block");
return a+25;
} catch (Exception e) {
System.out.println("test2 catch exception");
} finally {
return a+5;
}
}




控制台打印输出:

test2 try block 25

2.2 返回值为基本类型的包装类型(final)



public static void main(String[] args) {
System.out.println(test3());
}
//final类型(不可变)此时是基本类型的包装类型  try 中有return ,finally 中改变值 并返回 ,最终返回值 受finally里代码影响
public static Integer test3(){
Integer a =20;
try {
System.out.println("test3 try block");
return a+25;
} catch (Exception e) {
System.out.println("test3 catch exception");
} finally {
return a+10;
}
//return a;
}




控制台打印输出:

test3 try block 30

2.3 返回值为String类型(final)



public static void main(String[] args) {
System.out.println(test4());
}
//final类型(不可变)此时是String字符串类型  try 中有return ,finally 中改变值 并返回,最终返回值 受finally里代码影响
public static String  test4(){
String a ="中国";
try {
System.out.println("test4 try block");
return a+"_强大繁荣";
} catch (Exception e) {
System.out.println("test4 catch exception");
} finally {
a = a+"== 强盛!";
return a;
}
//return a;
}




控制台输出:

test4 try block 中国== 强盛!

3.若try代码块里面有return ,假设要return 的值是C,C为引用类型并且是没有被final修饰的可变类型,并且finally语句块里面 对try语句块里面要return 的值A做了修改 ,那么不论finally块里面是不是返回了C,函数最终的返回结果都会受到finally块中对C操作的影响。



//定义了一个类型Cat ,引用类型,且是可变类型 class Cat{
private String name;
private int age;

public Cat(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}






public static void main(String[] args) {
System.out.println(test5());
}

//对于引用类型对象(非final)的, try 中有return ,finally 中改变值 ,最终返回值 受finally里代码影响
public static Cat test5(){
Cat cat = new Cat("tom",12);
try {
return cat;
} catch (Exception e) {

} finally {
cat.setName("mouse");
cat.setAge(13);
}
return cat;
}




控制台输出:

Cat{name='mouse', age=13}



public static void main(String[] args) {
System.out.println(test6());
}

//对于引用类型对象(非final)的, try 中有return ,finally 中改变值 并return ,最终返回值 受finally里代码影响
public static Cat test6(){
Cat cat = new Cat("tom",12);
try {
return cat;
} catch (Exception e) {

} finally {
cat.setName("mouse");
cat.setAge(13);
return cat;
}
// return cat;
}




控制台输出:

Cat{name='mouse', age=13}

4.总结

当返回值为基本类型或者被final修饰的不可变类型时,基本类型返回的就是值,被final修饰的不可变类型,对其进行修改,就变成了另外一个对象了,对源对象没有影响;

当返回值为引用类型并且是没有被final修饰的类型时,返回的是该对象的地址,对该对象进行修改,那就真正的修改了;
http://www.cnblogs.com/lewis0077/p/5113406.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: