About the dark horse basics -- java proves that return is returned only after finally execution, and the return value cannot be changed in finally.

Source: Internet
Author: User

<A href = "http://www.itheima.com" target = "blank"> android training </a>, <a href = "http://www.itheima.com" target = "blank"> java training </a>, we look forward to communicating with you! After entering the registration system, download the basic questions and read the questions. So I studied/*** 6th: Write a class to prove that return is returned only after finally execution, and the return value cannot be changed in finally. * @ Author somebodydie **/I wrote it in the first place .. The following code intercepts data from the network. [java]/* proves that return is returned only after finally execution, and the return value cannot be changed in finally. */Class FinallyTest {public static int demo () {int a = 1; try {System. out. println ("try a:" + a); return a; // code block to be detected} finally // The statement {a = 5; System. out. println ("finally a:" + a); // return 15 ;}} public static void main (String [] args) {System. out. println ("demo:" + demo (); // call demo, if the print result is 1, finally cannot change the return value in try} the above Code method is similar to most of the proof methods on the Internet. The proof principle is: the print result returns 1, so finally cannot change the return value in try. First, print the content in try, print the finally content, and finally print the demo: 1. Therefore, the execution order is after return is finally, this only proves that finally cannot change the return value but cannot prove it. finally executes the reason before return: Print in finally and try is inside the method, and the demo should be printed normally: 1 print before, because the execution sequence of the print statement is different. My code: [java] public class Test6 {public static void main (String [] args) {// TODO Auto-generated method stub System. out. println (prove ();} public static int prove () {int I = 0; try {I ++; return I/0;} catch (Exception e) {e. printStackTrace ();} finally {I = 100; try {Thread. sleep (1000); // Let the thread sleep for 1 second} catch (InterruptedException e) {e. printStackTrace ();} System. out. println ("output content first");} return I/0 ;}} In finally, I set the current thread to sleep for one second. This is to prove that if finally execution must be before return, then the two return exceptions will be reported after the "output content first" statement is printed. But the actual running result is: java. lang. arithmeticException:/by zeroat com. itheima. test6.prove (Test6.java: 18) at com. itheima. test6.main (Test6.java: 11) Output Exception in thread "main" java. lang. arithmeticException:/by zeroat com. itheima. test6.prove (Test6.java: 30) at com. itheima. test6.main (Test6.java: 11) respectively comment out the try and the last return to conclude that the return in try should be executed before finally. After multiple operations, it is found that in most cases, the "output content first" statement is output before the last exception is reported. But sometimes this happens: java. lang. arithmeticException:/by zeroat com. itheima. test6.prove (Test6.java: 18) at com. itheima. test6.main (Test6.java: 11) Exception in thread "main" java. lang. arithmeticException:/by zeroat com. itheima. test6.prove (Test6.java: 30) at com. itheima. test6.main (Test6.java: 11) is the last return first or finally first output? Which return is used in the basic question? With these questions, it means that you are not easy to learn. There may be some low-level errors in the article. I hope you can point them out. It is also hoped that there will be great gods who will be able to prove which method to execute the last return and finally first.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.