有什么区别!=和=!在Java? [英] What is the difference between != and =! in Java?

查看:107
本文介绍了有什么区别!=和=!在Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看一些模拟的 OCJP 问题。我遇到了一个非常莫名其妙的语法。这是:

I was looking over some mock OCJP questions. I came across a really baffling syntax. Here it is:

class OddStuff {
    public static void main(String[] args) {
        boolean b = false;
        System.out.println((b != b));// False
        System.out.println((b =! b));// True
    }
}

为什么输出在!= =!

推荐答案

这个问题只是在玩你的间距令人困惑。

The question is just playing with you with confusing spacing.

b!= b 是通常的!= (不等于)比较。

b != b is the usual != (not equals) comparison.

另一方面:

b =! b 最好写成 b =!b ,其解析为:

b =! b is better written as b = !b which is parsed as:

b = (!b)

因此它是两个运营商。


  1. 首先反转 b

  2. 然后分配它返回 b

  1. First invert b.
  2. Then assign it back to b.

赋值运算符返回指定的值。因此,(b =!b)的计算结果为true - 这是您打印的内容。

The assignment operator returns the assigned value. Therefore, (b =! b) evaluates to true - which is what you print out.

这篇关于有什么区别!=和=!在Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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