I was reading my AP cs book and it talked about three types of variables:
我正在阅读我的AP cs书,它谈到了三种类型的变量:
•Instance variables
•实例变量
•Local variables
•局部变量
•Parameters
•参数
Instance variables are visible throughout the class etc... Parameters are only usable within the method and so are local variables . . .
实例变量在整个类等中都是可见的...参数仅在方法中可用,因此是局部变量。 。 。
Therefore my question is why would they classify Parameters and Local variables as different categories of variables if they contain the same scope. . . Despite the different uses of them.
因此,我的问题是,如果它们包含相同的范围,它们为什么会将参数和局部变量分类为不同的变量类别。 。 。尽管它们的用途不同。
3 个解决方案
#1
3
Because they don't necessarily have the same scope.
因为它们不一定具有相同的范围。
Take this case:
以这种情况为例:
// this is garbage code
public void doSomething(int foo) {
int meh = 0;
while (true) {
// can access foo and meh
int blah = meh++;
if (blah == foo) {
break;
}
}
// won't compile, can't access blah anymore
System.out.println(blah);
// will compile
System.out.println(foo);
// will compile as well
System.out.println(meh);
}
#2
2
The Java Language Specification identifies 7 types of variables. Their descriptions are
Java语言规范识别7种类型的变量。他们的描述是
A class variable is a field declared using the keyword static within a class declaration (§8.3.1.1), or with or without the keyword static within an interface declaration (§9.3).
类变量是在类声明(第8.3.1.1节)中使用关键字static声明的字段,或者在接口声明(第9.3节)中使用或不使用关键字static。
A class variable is created when its class or interface is prepared (§12.3.2) and is initialized to a default value (§4.12.5). The class variable effectively ceases to exist when its class or interface is unloaded (§12.7).
在准备其类或接口(第12.3.2节)时创建类变量,并将其初始化为默认值(第4.12.5节)。当类或接口被卸载时,类变量实际上不再存在(第12.7节)。
An instance variable is a field declared within a class declaration without using the keyword static (§8.3.1.1).
实例变量是在类声明中声明的字段,不使用关键字static(第8.3.1.1节)。
If a class T has a field a that is an instance variable, then a new instance variable a is created and initialized to a default value (§4.12.5) as part of each newly created object of class T or of any class that is a subclass of T (§8.1.4). The instance variable effectively ceases to exist when the object of which it is a field is no longer referenced, after any necessary finalization of the object (§12.6) has been completed.
如果类T具有作为实例变量的字段a,则创建新的实例变量a并将其初始化为默认值(第4.12.5节),作为每个新创建的类T对象的一部分或任何类的一部分。 T的子类(第8.1.4节)。在完成对象(第12.6节)的任何必要的最终化之后,当实例变量不再被引用时,实例变量实际上不再存在。
Array components are unnamed variables that are created and initialized to default values (§4.12.5) whenever a new object that is an array is created (§10, §15.10). The array components effectively cease to exist when the array is no longer referenced.
数组组件是未命名的变量,只要创建了一个新的数组对象(第10节,第15.10节),就会创建并初始化为默认值(第4.12.5节)。当不再引用数组时,阵列组件有效地停止存在。
Method parameters (§8.4.1) name argument values passed to a method.
方法参数(第8.4.1节)传递给方法的名称参数值。
For every parameter declared in a method declaration, a new parameter variable is created each time that method is invoked (§15.12). The new variable is initialized with the corresponding argument value from the method invocation. The method parameter effectively ceases to exist when the execution of the body of the method is complete.
对于方法声明中声明的每个参数,每次调用该方法时都会创建一个新的参数变量(第15.12节)。使用方法调用中的相应参数值初始化新变量。当方法体的执行完成时,方法参数有效地停止存在。
Constructor parameters (§8.8.1) name argument values passed to a constructor.
构造函数参数(第8.8.1节)将名称参数值传递给构造函数。
For every parameter declared in a constructor declaration, a new parameter variable is created each time a class instance creation expression (§15.9) or explicit constructor invocation (§8.8.7) invokes that constructor. The new variable is initialized with the corresponding argument value from the creation expression or constructor invocation. The constructor parameter effectively ceases to exist when the execution of the body of the constructor is complete.
对于构造函数声明中声明的每个参数,每次类实例创建表达式(第15.9节)或显式构造函数调用(第8.8.7节)调用该构造函数时,都会创建一个新的参数变量。使用创建表达式或构造函数调用中的相应参数值初始化新变量。当构造函数的主体的执行完成时,构造函数参数有效地停止存在。
An exception parameter is created each time an exception is caught by a catch clause of a try statement (§14.20).
每次try语句的catch子句捕获异常时,都会创建一个异常参数(§14.20)。
The new variable is initialized with the actual object associated with the exception (§11.3, §14.18). The exception parameter effectively ceases to exist when execution of the block associated with the catch clause is complete.
使用与异常关联的实际对象初始化新变量(第11.3节,第14.18节)。当与catch子句关联的块的执行完成时,异常参数实际上不再存在。
Local variables are declared by local variable declaration statements (§14.4).
局部变量由局部变量声明语句声明(第14.4节)。
Whenever the flow of control enters a block (§14.2) or for statement (§14.14), a new variable is created for each local variable declared in a local variable declaration statement immediately contained within that block or for statement.
只要控制流进入块(第14.2节)或语句(第14.14节),就会为在该块或for语句中立即包含的局部变量声明语句中声明的每个局部变量创建一个新变量。
You should also read about variable scope, which describes where some named entity can be used within your application.
您还应该阅读变量范围,它描述了在您的应用程序中可以使用某个命名实体的位置。
Therefore my question is why would they classify Parameters and Local variables as different categories of variables if they contain the same scope
因此,我的问题是,如果它们包含相同的范围,它们为什么会将参数和局部变量分类为不同的变量类别
As you can see from the descriptions above, they do not contain the same scope and therefore need to be differentiated.
从上面的描述中可以看出,它们不包含相同的范围,因此需要进行区分。
#3
0
Local variables are initialized in the method, while parameters are passed into the method.
局部变量在方法中初始化,而参数传递给方法。
public void method(int abc) //parameter
int xyz = 0; //local variable
#1
3
Because they don't necessarily have the same scope.
因为它们不一定具有相同的范围。
Take this case:
以这种情况为例:
// this is garbage code
public void doSomething(int foo) {
int meh = 0;
while (true) {
// can access foo and meh
int blah = meh++;
if (blah == foo) {
break;
}
}
// won't compile, can't access blah anymore
System.out.println(blah);
// will compile
System.out.println(foo);
// will compile as well
System.out.println(meh);
}
#2
2
The Java Language Specification identifies 7 types of variables. Their descriptions are
Java语言规范识别7种类型的变量。他们的描述是
A class variable is a field declared using the keyword static within a class declaration (§8.3.1.1), or with or without the keyword static within an interface declaration (§9.3).
类变量是在类声明(第8.3.1.1节)中使用关键字static声明的字段,或者在接口声明(第9.3节)中使用或不使用关键字static。
A class variable is created when its class or interface is prepared (§12.3.2) and is initialized to a default value (§4.12.5). The class variable effectively ceases to exist when its class or interface is unloaded (§12.7).
在准备其类或接口(第12.3.2节)时创建类变量,并将其初始化为默认值(第4.12.5节)。当类或接口被卸载时,类变量实际上不再存在(第12.7节)。
An instance variable is a field declared within a class declaration without using the keyword static (§8.3.1.1).
实例变量是在类声明中声明的字段,不使用关键字static(第8.3.1.1节)。
If a class T has a field a that is an instance variable, then a new instance variable a is created and initialized to a default value (§4.12.5) as part of each newly created object of class T or of any class that is a subclass of T (§8.1.4). The instance variable effectively ceases to exist when the object of which it is a field is no longer referenced, after any necessary finalization of the object (§12.6) has been completed.
如果类T具有作为实例变量的字段a,则创建新的实例变量a并将其初始化为默认值(第4.12.5节),作为每个新创建的类T对象的一部分或任何类的一部分。 T的子类(第8.1.4节)。在完成对象(第12.6节)的任何必要的最终化之后,当实例变量不再被引用时,实例变量实际上不再存在。
Array components are unnamed variables that are created and initialized to default values (§4.12.5) whenever a new object that is an array is created (§10, §15.10). The array components effectively cease to exist when the array is no longer referenced.
数组组件是未命名的变量,只要创建了一个新的数组对象(第10节,第15.10节),就会创建并初始化为默认值(第4.12.5节)。当不再引用数组时,阵列组件有效地停止存在。
Method parameters (§8.4.1) name argument values passed to a method.
方法参数(第8.4.1节)传递给方法的名称参数值。
For every parameter declared in a method declaration, a new parameter variable is created each time that method is invoked (§15.12). The new variable is initialized with the corresponding argument value from the method invocation. The method parameter effectively ceases to exist when the execution of the body of the method is complete.
对于方法声明中声明的每个参数,每次调用该方法时都会创建一个新的参数变量(第15.12节)。使用方法调用中的相应参数值初始化新变量。当方法体的执行完成时,方法参数有效地停止存在。
Constructor parameters (§8.8.1) name argument values passed to a constructor.
构造函数参数(第8.8.1节)将名称参数值传递给构造函数。
For every parameter declared in a constructor declaration, a new parameter variable is created each time a class instance creation expression (§15.9) or explicit constructor invocation (§8.8.7) invokes that constructor. The new variable is initialized with the corresponding argument value from the creation expression or constructor invocation. The constructor parameter effectively ceases to exist when the execution of the body of the constructor is complete.
对于构造函数声明中声明的每个参数,每次类实例创建表达式(第15.9节)或显式构造函数调用(第8.8.7节)调用该构造函数时,都会创建一个新的参数变量。使用创建表达式或构造函数调用中的相应参数值初始化新变量。当构造函数的主体的执行完成时,构造函数参数有效地停止存在。
An exception parameter is created each time an exception is caught by a catch clause of a try statement (§14.20).
每次try语句的catch子句捕获异常时,都会创建一个异常参数(§14.20)。
The new variable is initialized with the actual object associated with the exception (§11.3, §14.18). The exception parameter effectively ceases to exist when execution of the block associated with the catch clause is complete.
使用与异常关联的实际对象初始化新变量(第11.3节,第14.18节)。当与catch子句关联的块的执行完成时,异常参数实际上不再存在。
Local variables are declared by local variable declaration statements (§14.4).
局部变量由局部变量声明语句声明(第14.4节)。
Whenever the flow of control enters a block (§14.2) or for statement (§14.14), a new variable is created for each local variable declared in a local variable declaration statement immediately contained within that block or for statement.
只要控制流进入块(第14.2节)或语句(第14.14节),就会为在该块或for语句中立即包含的局部变量声明语句中声明的每个局部变量创建一个新变量。
You should also read about variable scope, which describes where some named entity can be used within your application.
您还应该阅读变量范围,它描述了在您的应用程序中可以使用某个命名实体的位置。
Therefore my question is why would they classify Parameters and Local variables as different categories of variables if they contain the same scope
因此,我的问题是,如果它们包含相同的范围,它们为什么会将参数和局部变量分类为不同的变量类别
As you can see from the descriptions above, they do not contain the same scope and therefore need to be differentiated.
从上面的描述中可以看出,它们不包含相同的范围,因此需要进行区分。
#3
0
Local variables are initialized in the method, while parameters are passed into the method.
局部变量在方法中初始化,而参数传递给方法。
public void method(int abc) //parameter
int xyz = 0; //local variable