PL4 declaring models (bindings, visibility, scope, and lifetimes)
1. Meaning of variables
In the teaching of programming languages, yqj2065 is often confused about whether metaphors should be used-for example, to liken variables to variable concepts in mathematics . I do not know from which old gentleman began, there is the saying: "Unlike constants, the value of the variable can be changed." If you are my student, also say, well, you come over, I promise not to kill you.
In assembly language, programmers start using variables such as AX, BX to represent the address where the operand is stored, and the address holds the actual value. a variable (variable) represents a storage location, which is a symbolic address that holds values in the source code .
Remember the difference between the variable and the value : The variable is the storage place, is a cup or box, age, Dog1 these variables (name) are the words printed on the cup, and the value is the cup of things (water, snacks or remote control).
From its value, I would rather say: A variable is a variable (its value), and the final variable is a variable whose value is immutable.
So, please, teachers, authors, when introducing variables, do not associate it with constants (terms such as "literal", literal or constant value in the introduction of programming-see 1.2.3).
Variables of reference types are called reference variables (reference variable) , such as DOG1 and name declared in dog Dog1 or string name, and they are allocated the same size of memory space , typically 32 bits ( Related to the implementation of the JVM). A variable of a reference type holds a value that is a reference value, referred to as a reference (reference). "After some simple conceptual problems are solved, there is no such confusion: whether the parameters of a method in Java can be passed by reference. 】
2. Binding of variables
such as: privatestring name;
publicstatic final int X = 123,y=456;
In the previous 2.2.1, we introduced the
1. Binding of variables to types。 Here is a simple introduction to the variable
declaration Statement (Declaration statements)Format.
"The naming of variables, in addition to following the general syntax of identifiers (such as not being able to start with numbers, *&^ punctuation), usually takes some kind of naming style. In the past, the Hungarian nomenclature, which was preceded by a variable name, was preceded by an abbreviation of its data type, such as iage, fnum, etc.; it is also suggested that all member variables should be prefixed with the M prefix and local variable plus _. "(I like to add this in my book)
3. Scope of variables
A variable's scope (scope) refers to the area of the source code that can access the variable, or the range or interval in which the variable functions in the program. This lexical scope (lexical scoping) was first introduced in Lisp1.5 and later added to the Algol language. By their influence, other languages have adopted the scope technology. In the source code, programmers prefer to indent individual scopes to enhance the readability of the code.
The most basic reason for using scopes is because the easy-to-use variable names are not as many as you might think,
With scopes, a program can have many of the same variable names, and they coexist peacefully--provided they have their own scopes. The scope of a variable is determined by the position of the declaration statement of the variable in the source code . (Many knowledge points are listed first, followed by a detailed explanation)
In Java, variables are divided into 4 types by scope, where they are declared and scoped as follows:
3 member variable/domain (member variable): Declared in the class body, whose scope is the entire class. Member variables include static and instance domains. For an instance domain, a static method or a static block is its restricted area, and it cannot be used in a static environment. When declaring a field, you can also define other characteristics of the variable. such as the access modifier defines whether other objects can access the variable, static identifies the variable as a static field, and final indicates that the variable is non-variable, and the transient variable is excluded when the object is serialized; Volatile is used to indicate that the value of a variable can be modified by different threads. As part of an object, the instance field is initialized at least to the default value when the object is created, and it can be used without the need to explicitly initialize it. When the class is loaded, the static domain is initialized.
3 formal parameter (formal arguments): Declared in the formal parameter list of a method or constructor whose scope is the entire method body of a method or constructor. A formal parameter is a placeholder that cannot be initialized with an assignment statement but instead relies on an argument to assign a value.
3 local variable: Declared in a code block of a method body or method body whose scope is from the start of the assignment to the first right curly brace that is subsequently encountered, "}". It is also easy to say that the scope of a local variable is from the defined statement to the end of the smallest block that encloses its definition (block). A programmer must explicitly initialize a local variable with an assignment statement in order to be able to use it in later code.
3 Exception Handling parameters (exception handler parameters): It is declared in a catch clause, in the general form of Try{}catch (Exception type exception handling parameter) {}. When an exception occurs in a try, the JVM matches the caught exception object with the parameter types of a series of catch clauses to find the code in the catch block that needs to be executed. Other aspects are the same as the formal parameters.
The variable is bound to its value, including variable initialization, assignment statements, and + + 、--operators. Assignment operations require type compatibility, and compatibility is guaranteed by the compiler's type checking.
4. Visibility and variable hidingThe scopes of the different variables can be nested, and the outer scopes contain the internal scopes. There are two issues directly related to the scope: 1. Where the code can access the variable, 2. Can access it with a simple variable name.
The visibility of variables is accessibility. The general rule is that variables are accessible within their scope, and as far as possible, they are inaccessible to programs that are outside the scope. The member variables of a class have different visibility depending on their access modifiers, but the instance variables are usually decorated as private so that they are not visible outside the class. Local variables (formal parameters, exception handling parameters) are visible only in their scope and cannot be used outside the scope.
In summary, the accessibility of variables is straightforward.
If there are two variables of the same name, so that a variable can not be directly accessed by the variable name, called variable hiding (hiding). There are several rules about variable hiding in Java:
3 A domain declared in a parent class, regardless of its access rights or whether it is inherited, whether static or instance, the subclass hides the domain with the same name in the parent class, and whether the data type is the same does not matter. See [6.3.2 Domain Inheritance and concealment] for examples.
A field in 31 classes must not have the same name.
3 Local variables of different scopes do not affect each other. Because they don't have anything to do with each other.
A variable of the same name is not allowed in the scope of 31 local variables, including formal parameters, because the Java language designer thinks that would confuse the program. For example, a local variable with the same name as the parameter is not allowed in the entire method body, or the compiler will give an error.
3 local variable hidden fields are discouraged. The scope of the domain is the entire class, where local variables can hide the domain in the independent kingdom in which the local variable is located. For example, dog has a domain int age, which defines a method foo () in dog, which declares the local variable age.
Voidfoo () {
int age=80;
System.out.println (age);
System.out.println (This.age);
}
Using the variable name age directly accesses a local variable, and the domain is hidden. However, the domain still exists and can be accessed using the This keyword. Local variable hidden fields are often considered bad programming styles.
3 The parameters in the set method are encouraged to hide the field.
The set method has parameters that are typically used to set the value of the field. The parameter of the Set method hides the field and is considered a good habit, thus eliminating the hassle of taking a different name for the equivalent variable.
5. Life cycleThe lifetime of a variable (lifetime) refers to the time at which the variable can be accessed when the program executes. Because the source code determines the operation of the program, the scope of the variable determines the lifetime of the variable.
In Java, the variables are divided into 3 types in terms of life time:
3 instance fields: Instance member variables are part of (the Class) object and each object has its own memory space to hold the instance domain. An object's instance field exists as long as the object exists. The new expression creates the object as the time the object was born and allocates memory in the Java heap. The destruction time of an object is managed by the garbage collector, and the programmer does not need to know its exact destruction time. Logically, an object can be considered destroyed if it is not pointed to by any reference.
3 static domain: A static domain belongs to a class, which is created when the class is loaded, and the static domain of the class exists as long as the class exists. It is shared by all objects and the object does not have its own copy.
3 local variable (and formal parameter, exception handling parameter): It is declared in a local (method body or block), so each time the declaration (first assignment) is executed, a local variable is created and the partial execution is destroyed. Local variables allocate memory in the stack.
Note that when discussing the lifetime of a variable, it is not considered whether the variable is a base type variable or a reference variable. Some people ask, where do basic type variables and reference variables allocate space? This is not an effective question.
The memory allocation to the variable does not look at the type of position, and the life of the variable does not look at the data type position. In summary, the object's space is on the heap, and the local variable space is on the stack. Both primitive and reference variables can be allocated in the stack or heap, and the key is to see where they are declared-whether it is a domain or a local variable.
such as Class A{inti; String s = "HI";},i and S as the domain of the object, having its own space in the heap.
and foo () {int i; String s= "HI";} I and s are used as local variables to allocate space on the stack.
The declaration model defines the criteria that a language must follow to declare identifiers, including bindings, scopes/scopes, lifetimes, and visibility . This section describes the declaration model as an example of variable declarations. Declaration of package and class name refer to [6th Chapter encapsulation], method definition reference [2.3.2 method has the same name problem].
"Introduction to Programming (Java) 2.2.3 Variable declaration model"