views:

4224

answers:

7
+5  A: 

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)

Kevin
Bear in mind that you can't define new operators in C++ either. All you can do is give new meanings to the old ones.
David Thornley
+2  A: 

Java does not have the ability to define new operators.

DJClayworth
+17  A: 

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

Apart from that, you can't define new operators in 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
javashlook
This is a **bitwise** XOR, and the OP is asking for a LOGICAL XOR.
Eddie
This escaped my memory too when I wrote my post, but I think you CAN use ^ as a logical operator (as well as bitwise).
Neil Coffey
@Eddie: Try "boolean a = true, b = false; System.out.println(a ^ b);"
Michael Myers
@mmyers: Yes, I know it works and can be used that way, and maybe that's why there is not an explicit logical XOR. I've used ^ myself in this fashion.
Eddie
@Eddie: Um, what did you just say? There is but there isn't?
Michael Myers
erickson
Eddie
Eddie
@Eddie: That and ^^ just looks too much like an emoticon.
Michael Myers
+11  A: 

Isn't it x != y ?

Maurice Perry
If x and y are booleans, then the logic table for xor and != are identical: t,t => f ; t,f => t; f,t => t; f,f => f
Greg Case
For booleans this works.
Eddie
Maurice: Arrgh you just blew my mind! How did I never notice this?
this only works with 2 booleans, not a list of booleans
Milhous
Milhous: if by a list you mean "a^b^c^d" (which doesn't even make sense) then != is fully equivalent.
Wow! that was a controversal one! I guess that ^ is a better answer if you are interested in boolean arithmetics, and that one is better to if you just want to compare two boolean values.
Maurice Perry
+2  A: 

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

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.

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.

TofuBeer
I gave you +1: but you should provide a reference to + on String overloading.
harschware
done - thx for the suggestion.
TofuBeer
+4  A: 

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

Wrong.

Java has

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

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

starblue
+4  A: 

Perhaps you misunderstood the difference between & and &&, | and || The purpose of the short cut 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 ^

Peter Lawrey