Home > Java > javaTutorial > Detailed explanation of the scope of Java variables and objects (picture and text)

Detailed explanation of the scope of Java variables and objects (picture and text)

黄舟
Release: 2017-03-29 11:01:48
Original
2414 people have browsed it

This article mainly introduces the relevant knowledge about the scope of Java variables and objects. It has a very good reference value. Let’s take a look at it with the editor.

Most programming languages ​​provide the concept of “Scope”.

For the name defined in the scope, the scope also determines its "visibility" and "existence time". In C, C++, and Java, scope is determined by the position of curly braces.

The scope of variables

Example:

{
  int x = 12;
  /* only x available */
  {
    int q = 96;
    /* both x & q available */
  }
  /* only x available */
  /* q “out of scope” */
}
Copy after login

Java uses a pair of braces as the scope of a statement block, called For scope, as a variable defined in the scope, it can only be used before the end of that scope.

You cannot write code like the following in Java:

 {
   int x = 12;
   {
     int x = 96; /* illegal */
   }
 }
Copy after login

The Java compiler will think that the variable has been defined, so the variable in the scope cannot be repeatedly defined, but in C and C++ it can "Hiding" a variable in a larger scope is allowed in C and C++, but not in Java because Java's designers believe that doing so confuses the program.

Let’s look at two more examples. In order to let everyone see the effect, screenshots are used here:

Let’s look at this code again, let’s think about a question, what are the grammatical errors in lines 11 and 12? The code is as follows:

If we exchange the positions, the code is as follows:

After leaving the scope, the memory space allocated by the variable will be reclaimed by the JVM, so there will be no syntax errors. However, the second way of writing name does not leave the {} scope, so there will be syntax errors.

The above variables are all local variables, so what will be the result if there are global variables? Let’s talk in code. The code is as follows:

If you carefully observe and think about the code, you can draw the following conclusion about the scope of the variable:

In The global

variable name

and the local variable name can have the same variable name under the same scope package. The local variable and the local variable under the same scope package cannot have the same variable name (the scope cannot Repeated naming). When using a variable, if you do not specify whether to use a global variable or a local variable, the default is to use the local variable. However, if the local variable exceeds its own scope, it will become invalid and be garbage collected by the JVM. Then You can rename this variable and use the latest defined local variable.

Object scope

Java objects do not have the same existence time as Zhu types. When you create a Java object using the

new

keyword, it will go out of scope. So if you use the following code:

Then the handle s, that is, the reference

will disappear at the end of the scope. However, the String object pointed to by s still occupies memory space. In the above code, we have no way to continue using this object because the only handle pointing to it has exceeded the boundaries of the scope. The result of this is: for objects created with new, they will remain as long as we want. This

Programming

problem is particularly prominent in C and C++. The biggest trouble is in C++: since you can't get any help from the language, you can't be sure whether objects are available when you need them. And the most troublesome thing is that in C++, once the work is completed, the object must be cleared manually.

This brings up an interesting question. If Java allows objects to remain the same, how can we prevent them from filling up memory and eventually causing the program to "freeze". In C++, this problem is the biggest headache for programmers. But after Java, the situation has changed. Java has a special "garbage collector" that looks for all objects created with new and identifies which of them are no longer referenced. It then automatically frees the memory occupied by those idle objects so that it can be used by new objects. This means we don't have to worry about memory reclamation at all. Simply create objects and they will go away automatically once they are no longer needed. Doing so prevents a common programming problem in C++: "memory overflow" caused by the programmer forgetting to release memory.

The above is the detailed content of Detailed explanation of the scope of Java variables and objects (picture and text). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template