如何在Java中的if语句中为变量赋值 [英] How can you assign a variable a value inside a if statement in Java

查看:179
本文介绍了如何在Java中的if语句中为变量赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做这样的事情,

if (first_var > second_var)
  int difference = first_var - second_var;
if (first_var < second_var)
  int difference = second_var - first_var;

当我尝试编译它时,会出现错误,说明变量差异可能尚未初始化。使变量差异全局也无济于事。

When I try to compile this, an error comes stating the variable 'difference' may not have been initialized. Making the variable 'difference' global doesn't help either.

推荐答案

您需要了解的问题包括:

The issues that you need to learn are:


  • 变量范围

  • 块声明以及如何创建新范围

  • 为什么你更喜欢使用 {...} 阻止如果陈述

  • 如何保证明确分配

  • 何时使用 if-else 而不是 if(something){...} if(!something){ ...}

  • The scope of variables
  • Block declaration and how that creates new scopes
  • Why you should prefer to use {...} block for if statements
  • How to guarantee definite assignment
  • When to use if-else instead of if (something) {...} if (!something) {...}

顺便说一下,找到两个值之差的惯用方法是:

By the way, the idiomatic way to find the difference of two values is:

int difference = Math.abs(firstVar - secondVar);

请注意 Math.abs 有一个极端情况:当参数为 Integer.MIN_VALUE 时,返回的值为 Integer.MIN_VALUE 。那是因为 -Integer.MIN_VALUE == Integer.MIN_VALUE 。这里的问题是数字的32位二进制补码表示。

Do note that Math.abs has a corner case: when the argument is Integer.MIN_VALUE, the returned value is Integer.MIN_VALUE. That's because -Integer.MIN_VALUE == Integer.MIN_VALUE. The issue here is 32-bit two's complement representation of numbers.

  • JLS 16 Definite Assignment
  • JLS 14.4.2 Scope of Local Variable Declarations
  • JLS 14.9.2 The if-then-else Statement
  • Math.abs(int)
  • Wikipedia/Two's complement (read: the most negative number)

这是一次修复片段的尝试,通过在之前声明变量,如果

Here's one attempt to fix the snippet, by declaring the variable before the if

int difference;
if (first_var > second_var) {
  difference = first_var - second_var;
}
if (first_var < second_var) {
  difference = second_var - first_var;
}

// note: difference is not definitely assigned here!
//           (but at least it's in scope!)

我们现在遇到问题明确赋值:如果 first_var == second_var ,变量差值仍然没有赋值。

We now have a problem with definite assignment: if first_var == second_var, the variable difference is still not assigned a value.

这是第二次尝试:

int difference = 0;
if (first_var > second_var) {
  difference = first_var - second_var;
}
if (first_var < second_var) {
  difference = second_var - first_var;
}

// note: difference is in scope here, and definitely assigned

初学者倾向于这样做,但这排除了使差异 最终局部变量的可能性,因为它可能被赋值两次。

Beginners tend to do this, but this precludes the possibility of making difference a final local variable, because it's possibly assigned value twice.

这是一个更好的尝试:

final int difference;
if (first_var > second_var) {
  difference = first_var - second_var;
} else {
  difference = second_var - first_var;
}

// note: difference is in scope, and is definitely assigned here,
//       (and declared final)

仍然有办法改善这一点。

There are still ways to improve on this.

一旦你对语言和编程更加熟悉,你可以使用以下习语:

Once you're more comfortable with the language and programming, you may use the following idiom:

final int difference = (first_var > second_var) ? first_var - second_var
                                                : second_var - first_var;

这使用?:三元/有条件运营商。小心这个操作员;它有一些令人惊讶的行为,它肯定会被滥用。明智地,谨慎地使用。

This uses the ?: ternary/conditional operator. Do be careful with this operator; it has some behaviors that may be surprising, and it definitely can be abused. Use carefully, judiciously, idiomatically.

这篇关于如何在Java中的if语句中为变量赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆