我只是好奇。我想知道是否有一個具體原因 「var&= expr」的行爲不像「var = var && expr」
不行爲的方式
var = var && expr.
看起來好像是第一個表達同樣被不管執行的表達
var &= expr
對var的錯誤值。
我正在使用Java 6,FYI。此代碼:
public class Test
{
protected static String getString()
{
return null;
}
public static void main(String... args)
{
String string = getString();
boolean test = (string != null);
test = test && (string.length() > 0);
System.out.println("First test passed");
test &= (string.length() > 0);
System.out.println("Second test passed");
}
}
給我:
First test passed
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Ref。 http://stackoverflow.com/questions/7101992/why-do-we-usually-use-not-what-is-the-difference,http://stackoverflow.com/questions/9264897/reason-for-the- exsistance-的,非短路邏輯運算符,http://stackoverflow.com/questions/4014535/vs-and-vs – 2013-03-08 22:48:46