一道非常考察java基础的题目

请说出main()方法的执行结果,以及每个方法执行完之后的打印结果,以及变量 i 最后的值是多少?

代码如下:

public class Test {

	public static int getI1() {
		int i = 0;
		try {
			i++;
			throw new RuntimeException();
		} catch (Exception e) {
			return i++;
		} finally {
			return i++;
		}
	}
	public static int getI2() {
		int i = 0;
		try {
			i++;
			throw new RuntimeException();
		} catch (Exception e) {
			return i++;
		} finally {
			i++;
		}
	}

	public static int getI3() {
		int i = 0;
		try {
			i++;
			throw new RuntimeException();
		} catch (Exception e) {
			i++;
			return i++;
		} finally {
			i++;
		}
	}

	public static int getI4() {
		int i = 0;
		try {
			i = i + 1;
			throw new RuntimeException();
		} catch (Exception e) {
			i++;
			return i++;
		} finally {
			i++;
			return i++;
		}
	}

	public static int getI5() {
		int i = 0;
		try {
			i = i + 1;
			throw new RuntimeException();
		} catch (Exception e) {
			i++;
			return i++;
		} finally {
			i++;
		}
	}

	public static int getI6() {
		int i = 0;
		try {
			i = i + 1;
			throw new RuntimeException();
		} catch (Exception e) {
			i++;
			return i++;
		} finally {
			return i++;
		}
	}

	public static int getI7() {
		int i = 0;
		return (i++ + i++);
	}

	public static int getI8() {
		int i = 0;
		return ((i++) + (i = i + 1) + (i++));
	}

	public static void main(String[] args) {
		System.out.println("getI1()=======" + Test.getI1());
		System.out.println("getI2()=======" + Test.getI2());
		System.out.println("getI3()=======" + Test.getI3());
		System.out.println("getI4()=======" + Test.getI4());
		System.out.println("getI5()=======" + Test.getI5());
		System.out.println("getI6()=======" + Test.getI6());
		System.out.println("getI7()=======" + Test.getI7());
		System.out.println("getI8()=======" + Test.getI8());
	}
}

      

          此题考察java的很多的基础知识:++、try catch finally语句、i++与i=i+1的区别、运算符的优先级、return语句的原理等。

 

 

 

 

 

 

 

 

 

 

 

 

   

 

 

 

答案:

 ----Test.getI1()打印结果是:2,最后的i值是2。  

 ----Test.getI2()打印结果是:1,最后的i值是3。  

 ----Test.getI3()打印结果是:2,最后的i值是4。 

 ----Test.getI4()打印结果是:4,最后的i值是4。  

 ----Test.getI5()打印结果是:2,最后的i值是4。  

 ----Test.getI6()打印结果是:3,最后的i值是3。

 ----Test.getI7()打印结果是:1,最后的i值是2。

 ----Test.getI8()打印结果是:4,最后的i值是3。  

 

 

 结论:

java 的异常处理中:在不抛出异常的情况下,程序执行完 try 里面的代码块之后,该方法并不会立即结束,而是继续试图去寻找该方法有没有 finally 的代码块:

        如果没有 finally 代码块,整个方法在执行完 try 代码块后返回相应的值来结束整个方法;

        如果有 finally 代码块,此时程序执行到 try 代码块里的 return 语句之时并不会立即执行 return,而是先去执行 finally 代码块里的代码,

        若 finally 代码块里没有 return 或没有能够终止程序的代码,程序将在执行完 finally 代码块代码之后再返回 try 代码块执行 return 语句来结束整个方法;

        若 finally 代码块里有 return 或含有能够终止程序的代码,方法将在执行完 finally 之后被结束,不再跳回 try 代码块执行 return。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值