||(Or) Logical Operator in Java vs .Net [closed]
I have been coding in Java(Mainly) and .Net for a while.
What I found is that the ||
logical operator in .Net is different in result to the ||
operator in Java.
Lets look at the following Java code:
Object obj =开发者_JAVA百科 null;
if(obj == null || obj.toString().isEmpty()){
System.out.println("Object is null");
}
The result of the code above will be:
Object is null
The reason for that is because obj == null
is true
and the second expression wasn't evaluated. If it was, I would have received a java.lang.NullPointerException
.
And if I used the single or (|
) I would also received a NullPointerException
(Both are evaluated).
My question is the following:
If the code was C#, I will always get a ObjectReferenceNotSet etc. exception because the obj value is null and the second expression is always evaluated (Regardless of the operator), meaning the result is different in C# than in Java. If I would to change the C# code to work properly, I have to create two if statements.
Is there not an easier way to do this in C# to be similar to Java? (Keep it in one if with 2 expressions)
Thank you.
The ||
operator in C# is short-circuiting, just like in Java. As is &&
. The |
and &
operators are not short-circuiting, just like in Java.
If your results are different, there is a bug in the code. Can you show us the offending C# please?
This works fine:
object obj = null;
if(obj == null || string.IsNullOrEmpty(obj.ToString())) {
Console.WriteLine("Object is null");
}
The || operator has exactly the same meaning in Java and C#. It is called a conditional logical OR, or "short-circuiting" logical OR operator:
http://msdn.microsoft.com/en-us/library/6373h346%28VS.71%29.aspx
This behaviour is a strict java language feature:
At run time, the left-hand operand expression is evaluated first ;[...] if the resulting value is true, the value of the conditional-or expression is true and the right-hand operand expression is not evaluated. If the value of the left-hand operand is false, then the right-hand expression is evaluated; [...] the resulting value becomes the value of the conditional-or expression.
Thus, || computes the same result as | on boolean or Boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always
Similar rules are defined for the java conditional-and operator.
Compare (identical) to C#:
The
&&
and||
operators are called the conditional logical operators. They are also called the “short-circuiting” logical operators.conditional-and-expression:
- inclusive-or-expression
- conditional-and-expression && inclusive-or-expression
conditional-or-expression:
- conditional-and-expression
- conditional-or-expression || conditional-and-expression
The
&&
and||
operators are conditional versions of the&
and|
operators:
- The operation
x && y
corresponds to the operationx & y
, except thaty
is evaluated only ifx
is notfalse
.- The operation
x || y
corresponds to the operationx | y
, except thaty
is evaluated only ifx
is not true.
It's called "short circuit evaluation". There is no need to evaluate the next statement. This is a nice feature in Java.
精彩评论