为什么可以声明同名的变量?

huangapple go评论38阅读模式
英文:

why can claim variables in same name?

问题

Java不允许使用相同名称的相同类型变量。

像这样的代码将会产生一个错误消息:"重复的局部变量"

public class test123 {
    public static void main(String[] args) {
        int a=1;
        int a=1;
    }
}

但是对于这段代码,"int a" 在 while 循环中被声明了三次(因为这个 while 循环运行了3次,所以 "int a" 也被声明了3次),为什么编译器允许它通过编译?

public static void main(String[] args)  {  
    int i=3;
    while (i>0) {
        int a=1;
        System.out.println("1");	
        i--;
    }
}
英文:

Java does not allow same type variable with same names.

Like this code will have an error message: "Duplicate local variable"

public class test123 {
	public static void main(String[] args) {
      int a=1;
	  int a=1;
	}  
}

but for this code, "int a" is claimed in the while loop three times(because this while loop run 3 times, so "int a" is claimed for 3 times also),

why the compiler allow it to pass compile?

public static void main(String[] args)  {  
	int i=3;
	while (i>0) {
      int a=1;
	  System.out.println("1");	
      i--;
     }
} 

答案1

得分: 2

The scope of the loop resets each iteration, so a is removed from the stack and able to be declared again.

Note: this is valid, and maybe what you wanted.

int a = 1;
a = 1;
英文:

The scope of the loop resets each iteration, so a is removed from the stack and able to be declared again.

Note: this is valid, and maybe what you wanted.

int a = 1;
a = 1;

答案2

得分: 1

"Variable declaration" 在编译时发生,而不是在运行时。即使在循环内声明变量,变量也只声明一次。

在运行时,只存在一个变量 "a" 的副本,它在循环迭代期间既不创建也不销毁。

英文:

"Variable declaration" happens at compile time, not at run time. Even if you declare a variable within a loop, the variable is declared only once.

At run time, only one copy of your variable "a" exists, and it's neither created nor destroyed during the loop iteration.

答案3

得分: 0

类作用域: 被定义为静态的变量称为类级别变量,它们只会为特定类创建一次,然后在该类的整个生命周期中使用。

实例作用域: 这些变量在类级别被定义为非静态的。变量的作用域仅限于特定实例。如果创建了一个类的两个实例,那么每个实例都会有自己的变量和存储空间。

局部作用域: 这些变量在方法内部定义,或者作为方法的参数。这些变量的作用域仅限于该方法。无法在方法外部访问它们。

块作用域: 这些变量在方法中的特定块(if、while、for、switch 等)内定义。该变量只能在该块内访问,无法在该块外部访问。

在你的情况下,变量 i 是局部变量,因此它只在该特定方法中可用。变量 a 是块变量,其作用域仅限于 while 循环的单次迭代。一旦开始执行 while 循环,它将创建变量 a,并且在第一次迭代执行结束后将销毁该变量。因此,在下一次迭代中将重新执行相同的过程。

英文:

There is totally 4 sope of a variable.

Class scope: The variable those defined as static are called class level variables and it will be created only once for a particular class and the same will be used throughout the life of that class.

Instance scope: These the variable which is defined as non-ststic at the class level. The scope of the variable if for that particular instance. If you create 2 instances of a class than each instance will have there own variables and storage.

Local scope: These variables are defined inside the method or as a parameter of a method. The scope of these variables is for that method only. It can't be accessed outside the method.

Block scope: These variables are defined inside a particular block (if, while, for, switch etc) in a method. And the variable is accessed only inside that block, it will be assessable outside that block.

In your case the variable i is a local variable so it will be available for that particular method. variable a is a block variable and the scope of that variable if only for that while loop single iteration. Once one it starts executing the while loop, it will create the variable a and once the execution of the first iteration finished than it will destroy that variable. SO in the next iteration again the same process will be followed.

huangapple
  • 本文由 发表于 2020年8月2日 12:50:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63212496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定