私はただ興味があります。具体的な理由があれば教えていただきたいです。
var &= expr
と同じように動作しません
var = var && expr.
var の false 値に関係なく、最初の式が実行されているようです。
私はJava 6を使用しています。参考までに。このコード:
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)