object == null或null == object? [英] object==null or null==object?

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

问题描述

我从某人那里听说 null == object 优于 object == null check

I heard from somebody that null == object is better than object == null check

例如:

void m1(Object obj ) {
   if(null == obj)  // Is this better than object == null ? Why ?
       return ;
   // Else blah blah
}

有什么理由或这是另一个神话?
感谢您的帮助。

Is there any reasons or this is another myth ? Thanks for help.

推荐答案

这可能是从C学习的习惯,以避免这种错字(单身) = 而不是双 == ):

This is probably a habit learned from C, to avoid this sort of typo (single = instead of a double ==):

if (object = null) {

== 左侧的常量在Java中并不实用,因为Java要求表达式为 if 求值为 boolean 值,所以除非常量是 boolean ,否则你会得到一个编译错误提出论点。 (如果它是一个布尔值,你不应该使用 == ...)

The convention of putting the constant on the left side of == isn't really useful in Java since Java requires that the expression in an if evaluate to a boolean value, so unless the constant is a boolean, you'd get a compilation error either way you put the arguments. (and if it is a boolean, you shouldn't be using == anyway...)

这篇关于object == null或null == object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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