开发者

||(Or) Logical Operator in Java vs .Net [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

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 operation x & y, except that y is evaluated only if x is not false.
  • The operation x || y corresponds to the operation x | y, except that y is evaluated only if x 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜