JAVA face question

Source: Internet
Author: User

First, let's look at some code.

int num=1;try{  num++;  return num;} finally{++num; SYSTEM.OUT.PRINTLN (num);}

Now let's parse this code and explore what the final num value is.

1. First by int num=1; This code we know that NUM has a value of 1;

2. Enter try to continue execution, num++ after the value of NUM becomes 2,return num after the system will automatically add a piece of memory in system memory, store the value of Num after return (2)

3. After finally (the finally block must be executed, unless the Java virtual machine exits without execution), the value of num is changed to 3 after ++num, but it returns the return NUM code again, just as mentioned in the second step, the memory of NUM's

The value is 2, so it outputs a value of num of 2;

JAVA face question

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.