哪个更有效:if(null == variable)or if(variable == null)?
在Java中哪个更有效,有什么区别?
if (null == variable) 要么
 if (variable == null) 
	
(类似于这个问题: null == object和object == null之间的区别 )
我会说,这两个expression式在性能上绝对没有区别。
然而有趣的是,编译后的字节码(由Sun javac发出)看起来有点不同。
 对于boolean b = variable == null : 
  3: aload_1 // load variable 4: ifnonnull 11 // check if it's null 7: iconst_1 // push 1 8: goto 12 11: iconst_0 // push 0 12: istore_2 // store 
 对于boolean b = null == variable : 
  3: aconst_null // push null 4: aload_1 // load variable 5: if_acmpne 12 // check if equal 8: iconst_1 // push 1 9: goto 13 12: iconst_0 // push 0 13: istore_2 // store 
 正如@Bozho所说, variable == null是最常见的,默认和首选的样式。 
 但是,在某些情况下,我倾向于将null置于前面。 例如在以下情况下: 
 String line; while (null != (line = reader.readLine())) process(line); 
 这就是所谓的“尤达条件” ,目的是防止您意外地使用赋值( = )而不是相等检查( == )。 
没有不同。
  if (variable == null)是(imo)更好的编程风格。 
 请注意, null是Java中的小写字母。 
没有不同
  (null == variables)有时被用在旧时代(C语言),以避免写错:(错误(variable = NULL) 
简短的回答:没有区别。
 较长的回答:有一些主观的风格差异。 有些人认为,常量应该在左侧作为一种防御风格,以防万一你错误==入= 。 有些人认为常量应该是正确的,因为它更自然可读。 
一个devise良好的语言与一个好的编译器和静态分析工具相结合,偏执可以被最小化,所以你应该写最可读和自然的代码,这将是常数在右边。
相关问题
下次请使用searchfunction。
- 空==对象和对象==空之间的区别
- null!= object和object!= null有什么区别?
- 哪种方式更好“null!= object”或“object!= null”?
- 为什么在C#中经常看到“null!= variable”而不是“variable!= null”呢?
- '…!= null'或'null!= …'performance最好?
 首先是从C挂起,这是完全合法的写if(variable = NULL) 
从性能的angular度来看,没有实质性的区别。
但是…如果你犯了一个错字,又错过了一个单一的等字符呢?
 foo = null; // assigns foo to null at runtime... BAD! 
与
 null = foo; // compile time error, typo immediately caught in editor, developer gets 8 hours of sleep 
这是赞成开始一个如果在左边的空testing的一个参数。
赞成开始iftesting的第二个参数是,即使在等号右边的expression式是冗长的时候,代码的读者也很清楚地看到他们正在查看一个空testing。
@aiooba也指出了第二个论点:
但是,在某些情况下,我倾向于将null置于前面。 例如在以下情况下:
 String line; while (null != (line = reader.readLine())) process(line); 
我的意见:不关心这种微不足道的性能优化。 如果你的性能不好,find并确定你的代码中真正的问题/瓶颈。
没有任何区别。