在 Java 中创建“逻辑异或”运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/726652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:45:59  来源:igfitidea点击:

Creating a "logical exclusive or" operator in Java

javaoperatorsxor

提问by eleven81

Observations:

观察:

Java has a logical AND operator.
Java has a logical OR operator.
Java has a logical NOT operator.

Java 有一个逻辑 AND 运算符。
Java 有一个逻辑 OR 运算符。
Java 有一个逻辑 NOT 运算符。

Problem:

问题:

Java has no logical XOR operator, according to sun. I would like to define one.

根据 sun 的说法,Java 没有逻辑 XOR 运算符。我想定义一个。

Method Definition:

方法定义:

As a method it is simply defined as follows:

作为一种方法,它的定义如下:

public static boolean logicalXOR(boolean x, boolean y) {
    return ( ( x || y ) && ! ( x && y ) );
}



Method Call:

方法调用:

This method is called in the following way:

该方法的调用方式如下:

boolean myVal = logicalXOR(x, y);



Operator Usage:

操作员用法:

I would much rather have an operator, used as follows:

我宁愿有一个运算符,使用如下:

boolean myVal = x ^^ y;



Question:

题:

I can't find anything on how to go about defining a new operator in Java. Where should I start?

我找不到有关如何在 Java 中定义新运算符的任何信息。我应该从哪里开始?

采纳答案by javashlook

Java does have a logical XOR operator, it is ^(as in a ^ b).

Java确实有一个逻辑 XOR 运算符,它是^(如a ^ b)。

Apart from that, you can't define new operators in Java.

除此之外,您不能在 Java 中定义新的运算符。

Edit:Here's an example:

编辑:这是一个例子:

public static void main(String[] args) {
    boolean[] all = { false, true };
    for (boolean a : all) {
        for (boolean b: all) {
            boolean c = a ^ b;
            System.out.println(a + " ^ " + b + " = " + c);
        }
    }
}

Output:

输出:

false ^ false = false
false ^ true = true
true ^ false = true
true ^ true = false

回答by Kevin Anderson

That's because operator overloading is something they specifically left out of the language deliberately. They "cheated" a bit with string concatenation, but beyond that, such functionality doesn't exist.

那是因为运算符重载是他们特意从语言中遗漏的。他们用字符串连接“欺骗”了一点,但除此之外,这样的功能不存在。

(disclaimer: I haven't worked with the last 2 major releases of java, so if it's in now, I'll be very surprised)

(免责声明:我没有使用过 Java 的最后两个主要版本,所以如果它现在在,我会非常惊讶)

回答by Maurice Perry

Isn't it x != y ?

不是 x != y 吗?

回答by TofuBeer

The only operator overloading in Java is + on Strings (JLS 15.18.1 String Concatenation Operator +).

Java 中唯一的运算符重载是字符串上的 +(JLS 15.18.1 字符串连接运算符 +)。

The community has been divided in 3 for years, 1/3 doesn't want it, 1/3 want it, and 1/3 doesn't care.

社区多年来一直分成3个,1/3不想要,1/3想要,1/3不在乎。

You can use unicode to create method names that are symbols... so if you have a symbol you want to use you could do myVal = x.$(y); where $ is the symbol and x is not a primitive... but that is going to be dodgy in some editors and is limiting since you cannot do it on a primitive.

您可以使用 unicode 创建作为符号的方法名称...所以如果您有一个想要使用的符号,您可以执行 myVal = x.$(y); 其中 $ 是符号,而 x 不是原语……但这在某些编辑器中会很狡猾,并且是有限制的,因为您不能在原语上执行此操作。

回答by starblue

Java has a logical AND operator.
Java has a logical OR operator.

Java 有一个逻辑 AND 运算符。
Java 有一个逻辑 OR 运算符。

Wrong.

错误的。

Java has

Java有

  • two logical AND operators: normal AND is & and short-circuit AND is &&, and
  • two logical OR operators: normal OR is | and short-circuit OR is ||.
  • 两个逻辑 AND 运算符:正常 AND 是 & 和短路 AND 是 &&,和
  • 两个逻辑 OR 运算符:正常 OR 是 | 和短路 OR 是 || 。

XOR exists only as ^, because short-circuit evaluation is not possible.

XOR 仅作为 ^ 存在,因为短路评估是不可能的。

回答by Peter Lawrey

Perhaps you misunderstood the difference between &and &&, |and ||The purpose of the shortcut operators &&and ||is that the value of the first operand can determine the result and so the second operand doesn't need to be evaluated.

也许你误解了差异之间&&&|||快捷运营商的目的&&,并||是第一个操作数的值可以决定结果,因此并不需要第二个操作数并进行评估。

This is especially useful if the second operand would results in an error. e.g.

如果第二个操作数会导致错误,这尤其有用。例如

if (set == null || set.isEmpty())
// or
if (list != null && list.size() > 0)

However with XOR, you always have to evaluate the second operand to get the result so the only meaningful operation is ^.

但是,对于XOR,您始终必须评估第二个操作数才能获得结果,因此唯一有意义的操作是^.

回答by iga

You can use Xtend (Infix Operators and Operator Overloading)to overload operators and 'stay' on Java

您可以使用Xtend(中缀运算符和运算符重载)来重载运算符并“停留”在 Java 上

回答by Timothy Jacobsen

Here is a var arg XOR method for java...

这是 java 的 var arg XOR 方法...

public static boolean XOR(boolean... args) {
  boolean r = false;
  for (boolean b : args) {
    r = r ^ b;
  }
  return r;
}

Enjoy

享受

回答by Shuliyey

You can just write (a!=b)

你可以写 (a!=b)

This would work the same as way as a ^ b.

这与a ^ b.

回答by Ledón

Because boolean data type is stored like an integer, bit operator ^ functions like a XOR operation if used with boolean values.

因为布尔数据类型像整数一样存储,如果与布尔值一起使用,位运算符 ^ 的功能类似于 XOR 操作。

//?Mfpl - XOR_Test.java

    public class XOR_Test {
        public static void main (String args[]) {
            boolean a,b;

            a=false; b=false;
            System.out.println("a=false; b=false;  ->  " + (a^b));

            a=false; b=true;
            System.out.println("a=false; b=true;  ->  " + (a^b));

            a=true;  b=false;
            System.out.println("a=true;  b=false;  ->  " + (a^b));

            a=true; b=true;
            System.out.println("a=true; b=true;  ->  " + (a^b));

            /*  output of this program:
                    a=false; b=false;  ->  false
                    a=false; b=true;  ->  true
                    a=true;  b=false;  ->  true
                    a=true; b=true;  ->  false
            */
        }
    }