在Java中,什么是布尔"操作顺序及QUOT ;? [英] In Java, what are the boolean "order of operations"?

查看:113
本文介绍了在Java中,什么是布尔"操作顺序及QUOT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们看一个物体的一个简单的例子。我想肯定了不空为橙色或灰色。

 如果(猫= NULL&放大器;!&安培; cat.getColor()==橙色|| cat.getColor()==灰色){
//做东西
}

我相信并且是第一位的,然后是OR。我有点模糊的,所以这里是我的问题:


  1. 有人能走我通过这句话,所以我敢肯定,我得到了什么情况?


  2. 另外,如果我加括号会发生什么;这是否改变操作的顺序?


  3. 请问我的操作顺序,可改变从语言到语言?



解决方案

Java教程有说明的运算符precedence 。平等的运营商将首先计算,那么&放大器;&安培; ,那么 || 。括号将任何事情之前进行评估,因此增加他们可以更改顺序。这通常是pretty多从语言到语言相同,但它总是一个好主意,仔细检查。

这是在行为上的微小变化,你不期望能引起你花了整整一天的调试,那么那么你肯定评价的顺序将是什么它是把括号的地方是个好主意。

Let's take a simple example of an object Cat. I want to be sure the "not null" cat is either orange or grey.

if(cat != null && cat.getColor() == "orange" || cat.getColor() == "grey") {
//do stuff
}

I believe AND comes first, then the OR. I'm kinda fuzzy though, so here are my questions:

  1. Can someone walk me through this statement so I'm sure I get what happens?

  2. Also, what happens if I add parentheses; does that change the order of operations?

  3. Will my order of operations change from language to language?

解决方案

The Java Tutorials has a list illustrating operator precedence. The equality operators will be evaluated first, then &&, then ||. Parentheses will be evaluated before anything else, so adding them can change the order. This is usually pretty much the same from language to language, but it's always a good idea to double check.

It's the small variations in behavior that you're not expecting that can cause you to spend an entire day debugging, so it's a good idea to put the parentheses in place so you're sure what the order of evaluation will be.

这篇关于在Java中,什么是布尔"操作顺序及QUOT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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