如果语句示例问题:在条件中遇到错误,而在某种情况下没有错误出现。

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

if statement example issue getting an error in condition and in one scenario no error appearing

问题

以下是翻译好的部分:

我正在尝试执行以下 if 语句代码,出现了一个错误,错误消息为“可能未对变量 j 进行初始化”。

示例 1:

    public class PrimitiveTypesChallenge {
    
       public static void main(String[] args) {
            
            int i = 10;
    		int j;
    		if(i == 10) {
    			j = 20;
    		}

    		System.out.println(j);
        }
    }

我在 if 块内为变量 `j` 初始化了一个值。

示例 2:

    public class PrimitiveTypesChallenge {
    
       public static void main(String[] args) {
            
            int i = 10;
    		int j;
    		if(i == 10) {
    			j = 20;	
    		}
            else {
               j = 30;
            }

    		System.out.println(j);
        }
    }

在这两个示例中,if 条件都为真,然而在示例 1 中出现了错误,在示例 2 中没有错误。我对这两种情况感到困惑。
有人可以帮我理解整个概念吗?
英文:

I am trying to execute the below if statement code, getting an error saying that "Variable j might not have been initialized".

Example 1:

public class PrimitiveTypesChallenge {

   public static void main(String[] args) {
        
        int i = 10;
		int j;
		if(i == 10) {
			j = 20;
		}

		System.out.println(j);
    }
}

I initialized value for j variable inside the if block.

Example 2:

public class PrimitiveTypesChallenge {

   public static void main(String[] args) {
        
        int i = 10;
		int j;
		if(i == 10) {
			j = 20;	
		}
        else {
           j = 30;
        }

		System.out.println(j);
    }
}

In both examples if conditions are true however in the example 1 getting an error and example 2 not getting an error. I am so confusing with these two scenarios.
Can someone help me to understand the whole concept behind this?

答案1

得分: 3

> 在示例1中,条件始终为真

_我们_知道这一点,但编译器并不足够聪明以发现这个事实。我们之所以知道,是因为我们读了代码并在脑中评估了表达式i == 10。但编译器并不会这样做。编译器可以评估常量表达式,而i == 10不是常量表达式,因为它涉及变量i

因此,编译器不知道在示例1中i == 10始终为真,因此它认为i == 10可能为假。在这种情况下,j将不会被初始化,因此会出现错误。这就是为什么在示例2中需要else分支来满足编译器要求。

您可以通过将i声明为final来使i == 10成为常量表达式:

final int i = 10;

这样,编译器就知道i的值是常量,j将始终被初始化,错误将消失。

英文:

> In example 1 the condition is always true

We know that, but the compiler is not smart enough to spot this fact. We know that because we read the code and evaluated the expression i == 10 in our heads. The compiler doesn't do this. Compilers can evaluate constant expressions, and i == 10 is not a constant expression, because it involves the variable i.

So the compiler does not know that i == 10 is always true in example 1, so it thinks that i == 10 could be false. In such a case, j would not be initialised, hence the error. This is why you need the else branch in example 2 to satisfy the compiler.

You can make i == 10 a constant expression by saying that i is final:

final int i = 10;

So the compiler knows that i's value is constant, and j will always be initialised, and the error will go away.

huangapple
  • 本文由 发表于 2020年5月4日 17:51:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/61589428.html
匿名

发表评论

匿名网友

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

确定