Java-preliminary Understanding-Chapter III-Partial code block

Source: Internet
Author: User

I. Definition of code blocks

Any curly braces are encapsulated, resulting in a chunk, which can be called a block of code. If you precede the code block with if (), then this is called the If code block;

public static void Main (string[] args), then this is called the main function code block. These are equivalent to the name of the code block. If nothing is added, then it is called a local code block.

Note : Why the above example can output an if statement of a, the conditional expression is false, why can it be set up?

Two. In-depth parsing of local code blocks

What is the name of the name of the code block, such as Class I code block, main function code block, if code block, if there is no statement before the code block, then it is called the local code block.

There are local code blocks, and there are local variables.

Example to parse a local variable. The following source code is running, DOS will error, display System.out.println ("over ..." +m) statement cannot find the symbol m,

The reason is that M is the variable in the above local code block, not suitable for the outside scope, the variable has its own scope. This is the role of a local code block without a name, although it is very similar to the sequential structure, but there is a difference.

The function of {} is to delimit the scope of action, only within parentheses.

Explore the rationale behind the example: the JVM executes to int m=89, when the memory will open a small section, named M, assigned to 89, with the end of the output statement and the arrival of the} symbol, a small block of memory named M is released, that is, there is no memory block called M. Why the M memory block will be released, because the range m can function is over. If you continue to persist, you will waste memory space. When the JVM runs to the SYSTEM.OUT.PRINTLN ("over ..." +m) statement, there is no memory block variable called M.

Here again to emphasize the role of the local code block: If the program defines a lot of variables, but the scope of the variable is very small, after the role here, the latter will no longer work, in order to save memory, we will be the operation of this variable encapsulated in the local code block, the operation to the back of time, this variable will be freed, saving memory It is also important to say that, although the computer memory is very large, but the virtual machine can operate the memory is not big, therefore, when necessary, to optimize the program.

A local code block can define the life cycle of a local variable.

By the concept of local code blocks, let's explore local variables.

Note: As long as the variables in the main function are called local variables.

Java-preliminary Understanding-Chapter III-Partial code block

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.