比较差异是什么? [英] What's the comparison difference?

查看:109
本文介绍了比较差异是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Null check in Java


我只是想知道有什么区别

  if(null == something){...} 

  if(something == null){。 ..} 

假设在这两种情况下 something 是同一个对象。除非是代码的可读性,否则应该是无效的。

解决方案

没有区别。
$ b

使用常量的想法有助于防止在条件中意外分配。例如在某些语言中,有效地说:

  if(i = 42){
...
}

i,条件为true。如果你不是这样做,没有编译器错误,很难找到。



如果你总是把常数放在前面:

  if(42 == i){
...
}
  

> if(42 = i){
...
}

编译器错误会立即提醒您尝试分配给常量。


Possible Duplicate:
Null check in Java

I'm just wondering what's the difference between

if (null == something) {...}

and

if (something == null) {...}

assuming that in both cases something is the same object. Presumably, there should be non, except for readability of the code.

解决方案

There is no difference.

The idea of putting the constant first helps guard against accidental assignment in the condition. For example in some languages it is valid to say:

if (i = 42) {
    ...
}

i is assigned and the condition is true. If you didn't mean to do this, there is no compiler error and it can be difficult to find.

If you instead always put the constant first:

if (42 == i) {
    ...
}

Then the day you accidentally do:

if (42 = i) {
    ...
}

A compiler error will alert you immediately that you are attempting to assign to a constant.

这篇关于比较差异是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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