这些你都可以做对你就是神

 

 

1:return 与 finally,下面的代码在finally块中加注释与不加注释两种情况分别输出什么呢,为什么你知道吗?
public class Main {

	public static void main(String[] args) throws Exception {

		System.out.println(Main.xxx());
	}

	public static int xxx() {
		int i = 0;
		try {
			return i;
		} finally {
			i = 1;
			//return i;
		}
	}

}

   在注释的情况下是 0 不注释的情况下 输出的是 1

 

2:

public class Main {


	 public static void main(String[] args) throws Exception {  
		  
	        System.out.println(Main.xxx());  
	    }  
	  
	    public static Long xxx() {  
	    	Long i = new Long(999999);  
	        try {  
	        	return i;  
	        } finally {  
	        	i=i+2;
	        }  
	    }  

}

     输出 999999 

 

 3:

public class Main {


	 public static void main(String[] args) throws Exception {  
		  
	        System.out.println(Main.xxx());  
	    }  
	  
	    public static HashMap<String,String> xxx() {  
	    	HashMap<String,String> i = new HashMap<String,String>();  
	        try {  
	        	i.put("1", "1");
	            return i;  
	        } finally {  
	        	i.put("2", "2");
	            //return i;  
	        }  
	    }  

}

  输出[[1,1],[2,2]]

 

     4:下面代码,在注释与不注释 x.setDaemon(true) 的情况下,分别输出什么?

 

	public static void main(String[] args) throws Exception {

		Thread x = new Thread() {
			public void run() {
				try {
					TimeUnit.SECONDS.sleep(3);
				} catch (InterruptedException e) {

				} finally {
					System.out.println("finally");
				}

			}
		};
		x.setDaemon(true);
		x.start();
		TimeUnit.SECONDS.sleep(2);

	}
}

   在注释的情况下是 finallly; 不注释的情况下什么也不输出

 

 

  5.是否可以捕获子线程抛出的异常呢,看下面代码?

 

 

public class Main {
	public static void main(String[] args) throws Exception {

		Thread x = new Thread() {
			public void run() {
				throw new RuntimeException();
			}
		};

		try {
			x.start();
		} catch (Exception e) {
			System.out.println("got Exception");
		}
	}
}
上述代码是不会输出 got Exception的,你做对了没有?
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值