WTF - Java XOR

Categories: Java, Off-topic

I love The Daily WTF site. Recently I came across some code that, in my opinion, qualifies.

The developer of the following fragment of code from a Java-based banking application has an obsession with the Java boolean-xor operation.

Interestingly, many Java tutorial websites don’t even know that such a thing exists (though it is defined in the Java language specification).

Lets start with a simple example from the code:

  if ((transaction.getCode().equals("181") ^ transaction.getCode().equals("G5E"))

Here the value being tested is the same on both sides, so a truth table has only three options:

  • when code=181: true xor false –> true
  • when code=G5E: false xor true –> true
  • when code=other: false xor false –> false

So in this case, the xor is identical to the more conventional OR operator (“||”).

Now try this more advanced example:

        if ((transaction.getFlgReceipts() != null && transaction.getFlgPrintRec() != null)
                && (transaction.getFlgReceipts().equals("B") && transaction.getFlgPrintRec().equals("Y"))
                        ^ (transaction.getFlgReceipts().equals("I") && transaction.getFlgPrintRec().equals("Y"))
                        ^ (transaction.getFlgReceipts().equals("B") && transaction.getFlgPrintRec().equals("T"))
                        ^ (transaction.getFlgReceipts().equals("I") && transaction.getFlgPrintRec().equals("T"))) {

            if ((transaction.getFlgReceipts().equals("B") || transaction.getFlgReceipts().equals("I"))
                    && (!Strings.isEmpty(transaction.getText1()) || !Strings.isEmpty(transaction.getText2())
                            || !Strings.isEmpty(transaction.getText3()))) {
                if (transaction.getText1() != null) {
                if (transaction.getText2() != null && !Strings.isEmpty(transaction.getText2())) {

                    if (!Strings.isEmpty(transaction.getText3())) {
            } else {
                if (transaction.getBigALP().length() > 34) {
                    bookingtext.add(transaction.getBigALP().substring(0, 34).trim());
                } else {



The ridiculously complicated if-structure is there just for amusement, as are the repeated getter invocations. The interesting part is the chain of xors in the first conditional clause.

As it happens, a chain of xors is true iff an odd number of inputs are true. In particular, it is:

  • false if all terms are false (zero true terms)
  • true if exactly one of the terms is true

Here the terms are mutually exclusive: no two can be true at the same time, ie either zero or one of the terms is true, never more. So, yes, here the xor is also equivalent to an or.

As a bonus, later in the same file, there is similar enthusiasm for regular expressions in Java:

    private List<String> performXSSPrevention(List<String> bookingtext) {

        final String replaceSEQUENCE = "</|/>|-->|\'>|\">|<!|[iI][mM][gG]|[hH][tT][tT][pP]|[sS][cC][rR][iI][pP][tT"
                + "]|[sS][rR][cC]|[cC][oO][oO][kK][iI][eE]|[aA][lL][eE][rR][tT]|[dD][oO][cC"
                + "][uU][mM][eE][nN][tT]\\.|[hH][iI][sS][tT][oO][rR][yY]\\.|[iI][fF][rR][aA" + "][mM][eE]|//";

        for (int i = 0; i < bookingtext.size(); i++) {
            if (!Strings.isEmpty(bookingtext.get(i))) {
                bookingtext.get(i).replaceAll(replaceSEQUENCE, " ");
        return bookingtext;


The bank in question shall remain unnamed. The developer’s employer, however, is consulting firm Accenture. Apparently this is not a case of “you get what you pay for”…