Analysis on the difference between Java and C + + language in scope

Source: Internet
Author: User
Tags valid variable scope

Java and C + + programming ideas Although there is a certain commonality, but in many ways there are still different. There are still great differences between the two in the scope. The following is an analysis of the differences in the scope of the two languages, the scope mentioned here includes variable scope and object scope.

Difference One: Variable scope difference

As the following program code is in accordance with the syntax requirements of C + + language. It can operate normally under C language. But when it is compiled on the Java language platform, it is told that there are errors. There are no errors on the top of the format or keywords. So what's wrong with that? This is related to the scope of the variable.

{
     float y=3.15

     {

     float y=3.15

    }

}

The scope determines the visibility and lifecycle of the variable name that it defines. In the C + + language, including the other languages it derives from, and in the Java language, there are curly braces that represent a scope. As the above code indicates, there are two scopes. The outer pair of curly braces represents a first-level scope; inside a pair of curly braces represents a level two scope, and so on. In general, variables defined in the first-level scope are valid for their subordinate rates. That is, its subordinate scope can directly refer to variables defined in the ancestor scope. However, in a level two scope, you can change the value of a variable set at the primary rate, but this change is only valid within a level two scope. As the above code, you can redefine a Y variable in a level two scope, and then assign a value. Note that the variables defined in the level two scope are the same as the variables defined in the first level scope, but they are not the same variable. If there are other codes referencing this y variable at this time, the value is still 3.14, not 3.15. That is, variables defined in a level two scope are valid only within them. There is no effect on the superior scope. That is, variables defined in the scope are valid only within their scope. Outside the scope of its function, its internal set of variables are all invalid.

The setting of this scope is actually similar to the Java language and the C + + language. But in detail the Java language has one more limit. As the code in the preceding table shows, although variable y is defined in two different scopes in the above code, it is supposed to be a mutual non-interference and coexistence. However, this is not allowed in the Java compiler. By compiling the above code in the Java compiler, the compiler notifies the program developer that the variable y has already been defined. Then the compilation will end in error. Although it is permissible to define a variable whose name is the same in a different level of scope in the C + + language, and not in the Java language, this does not mean that the variable is scoped. In essence, the scope of the two variables is the same. Just for the Java language, it adds a limit. In the Java language, variable names that are defined cannot be the same even if the scope is different. This is mainly to improve the readability of Java code, to prevent confusion to define this rule.

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.