||(或)Java 与 .Net 中的逻辑运算符

发布于 2024-10-04 23:11:41 字数 857 浏览 0 评论 0原文

我已经用 Java(主要)和 .Net 进行编码有一段时间了。

我发现.Net 中的 || 逻辑运算符与 Java 中的 || 运算符的结果不同。

让我们看一下下面的 Java 代码:

Object obj = null;

if(obj == null || obj.toString().isEmpty()){
    System.out.println("Object is null");
}

上面代码的结果将是:

Object is null

原因是因为 obj == nulltrue< /code> 并且第二个表达式未被评估。如果是这样,我就会收到 java.lang.NullPointerException

如果我使用单个或 (|),我也会收到一个 NullPointerException (两者都会被评估)。

我的问题如下:

如果代码是 C#,我将始终收到 ObjectReferenceNotSet 等异常,因为 obj 值为 null 并且始终计算第二个表达式(无论运算符如何),这意味着结果C# 中与 Java 中不同。 如果我想更改 C# 代码以使其正常工作,我必须创建两个 if 语句。

是否有更简单的方法可以在 C# 中执行此操作,类似于 Java ? (将其保留在一个 if 和 2 个表达式中)

谢谢。

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 = 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

于我来说 2024-10-11 23:11:41

C# 中的 || 运算符是短路运算符,就像 Java 中一样。正如 && 一样。 |& 运算符不是短路的,就像在 Java 中一样。

如果结果不同,则代码中存在错误。您能给我们展示一下有问题的 C# 吗?

这工作正常:

    object obj = null;

    if(obj == null || string.IsNullOrEmpty(obj.ToString())) {
        Console.WriteLine("Object is null");
    }

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");
    }
唱一曲作罢 2024-10-11 23:11:41

||运算符在 Java 和 C# 中的含义完全相同。它称为条件逻辑 OR,或“短路”逻辑 OR 运算符:

http://msdn.microsoft.com/en-us/library/6373h346%28VS.71%29.aspx

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

沒落の蓅哖 2024-10-11 23:11:41

此行为是严格的 Java 语言功能

在运行时,左侧操作数
首先计算表达式 ;[...] 如果结果值为
true,条件或的值
表达式为真且右手
不计算操作数表达式。
如果左侧操作数的值
为假,则右手
表达式被求值; [...] 这
结果值变为
条件或表达式。

因此,||计算结果与 | 相同
在布尔或布尔操作数上。它
不同之处仅在于右手
计算操作数表达式
有条件地而不是总是

定义类似的规则 java 条件-和运算符

与 C# 比较(相同):

&&|| 运算符被称为
条件逻辑运算符。
他们也被称为
“短路”逻辑运算符。

条件和表达式

  • 包含或表达式
  • 条件和表达式 &&包含或表达式

条件或表达式:

  • 条件和表达式
  • 条件或表达式 ||条件和表达式

&&|| 运算符是条件版本
&| 运算符:

  • 运算x && y 对应于
    操作x & y,除了 y
    仅当 x 不为 false 时才进行评估。
  • 操作x || y 对应于
    操作x | y,除了 y
    仅当 x 不为 true 时才进行评估。

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.
迷荒 2024-10-11 23:11:41

这称为“短路评估”。无需评估下一条语句。这是 Java 中的一个很好的功能。

It's called "short circuit evaluation". There is no need to evaluate the next statement. This is a nice feature in Java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文