Scope differences between Java and C ++

Source: Internet
Author: User

Java and C ++ are currently the most popular programming languages. Although the two programming ideas have some common characteristics, there are still large differences in many aspects. For example, the two are still very different in scope. Next, I will analyze the differences in the scope of these two languages to help you further understand the advantages of Java.

  Difference 1: Variable scopes are different.

For example, this program code meets the syntax requirements of the C ++ language. It can run normally in C language. However, when it is compiled on the Java platform, it will be notified of errors. The format and keywords are correct. So what is the error? This is related to the scope of the variable.

{

Flowat y = 3.15

{

Flowat y = 3.15

}

}

The scope determines the visibility and lifecycle of the variable names it defines. Both the C ++ language (including other derived languages) and Java language use curly braces to represent a scope. The code above indicates that there are two scopes. A pair of curly braces on the outside represents a level-1 Scope, and a pair of curly braces on the inside represents a level-2 scope, and so on. Generally, variables defined in the first-level scope are effective for their lower-level performance. That is to say, its lower-level scope can directly reference the variables defined in the upper-level scope. However, in the second-level scope, you can change the variable value set in the first-level effect rate, but this change is only valid within the second-level scope. As shown in the above Code, another y variable can be defined in the second-level scope and assigned a value again. Note: The variables defined in the second-level scope are not the same as those defined in the first-level scope. If other code references the y variable out of the first-level scope, the value is still 3.14 instead of 3.15. That is to say, the variables defined in the second-level scope are valid only within the second-level scope. There is no impact on the upper-level scope. That is to say, the variables defined in the scope are valid only within the scope. Out of its scope, all the variables set internally are invalid.

The setting of this scope is actually similar to that of C ++. However, in terms of details, the Java language has another limit. As shown in the code in the above table, although variable Y is defined in two different scopes in the above Code, the two are mutually independent and can coexist. However, this situation is not allowed in the Java compiler. If you compile the above Code in the Java compiler, the compiler will notify program developers that the variable Y has been defined. Then the compilation will end with an error. Although it is allowed to define variables with the same name in different levels of scopes in C ++, but not in Java, this does not mean that the scope of variables is different. In essence, the two variables have the same scope. It only adds a limit to the Java language. In Java, even if the scopes are different, the variable names defined by the variables cannot be the same. This rule is defined to improve the readability of Java code and prevent confusion.

  Difference 2: Differences in object scopes.

Java and C ++ are both object-oriented languages. However, the two have very different implementation mechanisms. There is a big difference in the scope of objects. This also leads to a huge difference in implementation details between the two object-oriented languages.

First, Java Programmers need to understand that the Java object scope and variable scope are different. In the above analysis, the scope of the variable is only valid within the scope. For example, if a variable defined in the second-level scope exceeded the second-level scope, it will be invalid. But the object is different, and it can exist outside the scope. For example, a name_full object is defined in a specific scope. When it is out of this scope, the reference of this object disappears. However, the created object is actually saved in the memory. During the Java program running process, as long as the means of passing or copying object references are passed, the object can still be accessed in other scopes. That is to say, as long as we have this need, the objects created in a specific scope will always exist and can be accessed anywhere outside the scope. Of course, the premise is to pass the object reference to other scopes by means of replication or transmission. This is the biggest difference between a Java object and a Java variable in scope.

Second, the Java object and the C ++ language have different scopes. In fact, the objects in the C ++ language are very similar to the scope of variables. Once an object is used in C ++, it must be destroyed. To be exact, it is necessary to release the memory space occupied by the used objects within the scope. Otherwise, if the object is acting on the outside, because it has lost its reference to this object, the object seems to be a person in space and can no longer perform any operations on it, you only need to let yourself go. For C ++, it is difficult for programmers to make sure that they can still access this object when they need to call the object after they leave the scope. This is also the biggest headache in the C ++ language development process. Because the object needs to be destroyed manually. If the memory space occupied by the object is not released in time, the security and performance of the application will be greatly affected.

In Java programs, objects out of this scope eventually have two destinations. First, the object can be accessed outside the scope by copying or passing it. Second, destroy the object. However, we do not need to use code to destroy the previously created object. In Java, there is a processing mechanism called the Garbage Collector, which can be used to dynamically monitor all objects created by the New Keyword and determine which objects will not be referenced based on certain rules. If it determines that an object is no longer referenced, the memory space occupied by these objects will be automatically released for other new objects. Our program developers only need to create objects without worrying about when to destroy them. Therefore, the memory overflow problem faced by C ++ can be eliminated. This memory overflow problem occurs when the developer forgets to destroy the objects after they are used up.

In Java program development, developers do not need to worry about object destruction. Therefore, they can focus more on code optimization. Unlike the C ++ language, the destruction of this object should be treated as the first major event. Even so, Java Programmers cannot take it lightly. Especially when we still need to access this object after leaving a specific scope, we must remember to pass the object reference to other scopes by means of replication or transmission. Otherwise, even if the object is not extinct, it is actually stored in the memory, but it cannot be accessed due to the lack of references. Therefore, when an object is referenced across scopes, the replication and transfer of this reference cannot be forgotten.

If you forget it, code out of scope will no longer be able to access this object. However, this object will be recycled by the garbage collector. The released memory space can be reused to prevent memory overflow.

In short, when Java programmers develop applications, the difference between the Java object and the Java variable scope, and the difference between the Java object and other language object scopes must be clear. This helps Java programmers make better use of this Java object. In addition, the author does not bother to emphasize it again. By default, after being out of a certain scope, the object will lose the reference and cannot be accessed. If you want to access this object again in the scope, You need to copy or pass the reference in time. Otherwise, code outside the scope cannot operate on this object.

Recommended for beginners a programming technology learning site, 96 stack Software Programming Network, http://www.96dz.com, which has c ++ video tutorial, C # video tutorial, Java video tutorial download, c \ c ++, Java, and C #. net and other programming technology abstracts, including the current mainstream Linux programming and web programming learning materials video tutorial download.

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.