为什么Java没有条件和条件运算符的复合赋值版本? (&& =,|| =) [英] Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)

查看:138
本文介绍了为什么Java没有条件和条件运算符的复合赋值版本? (&& =,|| =)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此对于布尔值的二元运算符,Java有& | ^ && ||

So for binary operators on booleans, Java has &, |, ^, && and ||.

让我们在这里简要总结一下他们的做法:

Let's summarize what they do briefly here:

  • JLS 15.22.2 Boolean Logical Operators &, ^, and |
  • JLS 15.23 Conditional-And Operator &&
  • JLS 15.24 Conditional-Or Operator ||

对于& ,如果两个操作数值都是 true ,则结果值为 true ;否则,结果是 false

For &, the result value is true if both operand values are true; otherwise, the result is false.

对于 | ,如果两个操作数值都是 false ,则结果值为 false ;否则,结果是 true

For |, the result value is false if both operand values are false; otherwise, the result is true.

对于 ^ ,如果操作数值不同,则结果值为 true ;否则,结果是 false

For ^, the result value is true if the operand values are different; otherwise, the result is false.

&& 运算符类似于& 但仅当其左侧操作数的值为 true

The && operator is like & but evaluates its right-hand operand only if the value of its left-hand operand is true.

|| 运算符类似于 | ,但仅当其左侧操作数的值为 false 时才计算其右侧操作数。

The || operator is like |, but evaluates its right-hand operand only if the value of its left-hand operand is false.

现在,在所有5个中,有3个具有复合赋值版本,即 | = & = ^ = 。所以我的问题很明显:为什么Java不提供&& = || = ?我发现我需要的不仅仅是我需要的& = | =

Now, among all 5, 3 of those have compound assignment versions, namely |=, &= and ^=. So my question is obvious: why doesn't Java provide &&= and ||= as well? I find that I need those more than I need &= and |=.

我不认为因为它太长是一个很好的答案,因为Java有>>> = 。这个遗漏必须有更好的理由。

And I don't think that "because it's too long" is a good answer, because Java has >>>=. There must be a better reason for this omission.

来自 15.26分配运算符


有12个赋值运算符; [...] = * = / =%= + = - =<< =>> =>>> =& = ^ = | =






评论如果& ;& = || = 已实施,那么它将是唯一不首先评估右侧的运营商。我相信复合赋值运算符首先评估右侧的概念是错误的。


A comment was made that if &&= and ||= were implemented, then it would be the only operators that do not evaluate the right hand side first. I believe this notion that a compound assignment operator evaluates the right hand side first is a mistake.

来自 15.26.2复合赋值运算符


形式 E1 op = E2 的复合赋值表达式相当于 E1 =(T)((E1)op (E2)),其中 T E1 的类型,但<$除外c $ c> E1 仅评估一次。

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

作为证据,以下代码段抛出 NullPointerException ,而不是 ArrayIndexOutOfBoundsException

As proof, the following snippet throws a NullPointerException, not an ArrayIndexOutOfBoundsException.

    int[] a = null;
    int[] b = {};
    a[0] += b[-1];


推荐答案

原因



运算符&& = || = Java上不可用因为对于大多数开发人员来说,这些运营商是:

Reason

The operators &&= and ||= are not available on Java because for most of the developers these operators are:


  • 容易出错

  • 无用的

如果Java允许&& = 运算符,那么该代码:

If Java allowed &&= operator, then that code:

bool isOk = true; //becomes false when at least a function returns false
isOK &&= f1();
isOK &&= f2(); //we may expect f2() is called whatever the f1() returned value

等同于:

bool isOk = true;
if (isOK) isOk = f1();
if (isOK) isOk = f2(); //f2() is called only when f1() returns true

第一个代码是容易出错,因为许多开发人员会认为无论f1()返回的值是什么,总是会调用 f2()。它就像 bool isOk = f1()&& f2(); 其中 f2()仅在 f1()返回时被调用

This first code is error-prone because many developers would think f2() is always called whatever the f1() returned value. It is like bool isOk = f1() && f2(); where f2() is called only when f1() returns true.

如果开发人员只想在 f1()<时调用 f2() / code>返回 true ,因此上面的第二个代码不易出错。

If the developer wants f2() to be called only when f1() returns true, therefore the second code above is less error-prone.

Else & = 就足够了,因为开发人员希望始终调用 f2()

Else &= is sufficient because the developer wants f2() to be always called:

bool isOk = true;
isOK &= f1();
isOK &= f2(); //f2() always called whatever the f1() returned value

此外,JVM应该运行这个以上代码如下:

Moreover, the JVM should run this above code as the following one:

bool isOk = true;
if (!f1())  isOk = false;
if (!f2())  isOk = false;  //f2() always called



比较&& & 结果



运营商的结果是&& ; & 在布尔值上应用时相同吗?

Compare && and & results

Are the results of operators && and & the same when applied on boolean values?

让我们检查一下以下Java代码:

Let's check using the following Java code:

public class qalcdo {

    public static void main (String[] args) {
        test (true,  true);
        test (true,  false);
        test (false, false);
        test (false, true);
    }

    private static void test (boolean a, boolean b) {
        System.out.println (counter++ +  ") a=" + a + " and b=" + b);
        System.out.println ("a && b = " + (a && b));
        System.out.println ("a & b = "  + (a & b));
        System.out.println ("======================");
    }

    private static int counter = 1;
}

输出:

1) a=true and b=true
a && b = true
a & b = true
======================
2) a=true and b=false
a && b = false
a & b = false
======================
3) a=false and b=false
a && b = false
a & b = false
======================
4) a=false and b=true
a && b = false
a & b = false
======================

因此我们可以通过& && $ c>表示布尔值;-)

Therefore YES we can replace && by & for boolean values ;-)

因此最好使用& = 而不是&& =

&& = 的原因相同:

运算符 | = || = 更不容易出错。

Same reasons as for &&=:
operator |= is less error-prone than ||=.

如果开发人员想要 f2() f1()返回 true ,然后我建议以下备选方案:

If a developer wants f2() not to be called when f1() returns true, then I advice the following alternatives:

// here a comment is required to explain that 
// f2() is not called when f1() returns false, and so on...
bool isOk = f1() || f2() || f3() || f4();

或:

// here the following comments are not required 
// (the code is enough understandable)
bool isOk = false;
if (!isOK) isOk = f1();
if (!isOK) isOk = f2(); //f2() is not called when f1() returns false
if (!isOK) isOk = f3(); //f3() is not called when f1() or f2() return false
if (!isOK) isOk = f4(); //f4() is not called when ...

这篇关于为什么Java没有条件和条件运算符的复合赋值版本? (&amp;&amp; =,|| =)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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