I learnt that Java does not give provision to programmer to overload an operator.
I also learnt that +
is the only operator overloaded in java,
But,
Operators &
|
^
work on integral operands as bitwise operators.
Operators &
|
^
work on boolean operands as logical operators.
My question:
Can I say that operators
&
^
|
are overloaded?Is my jargon correct on calling these operators as bitwise operators on integral operands and logical operators on boolean operands?
Because we tend to view numbers as just numbers, we often don't recognise arithmetic operators to be overloaded, despite the obvious fact that an integer division is very different from a floating point division.
So technically all the arithmetic operators are very much overloaded.
The easiest way to determine what's overloaded is to imagine that instead of the + operator you had to call
Operators.plus()
. When that method needs to be overloaded to deal with different inputs, so will your operator.So for example when you use
+
in a string concatenation sense, that would be equivalent toOperators.plus(Object first, Object second)
, whereas an integer addition would be covered byOperators.plus(int first, int second)
, and so on.(However, as a thought-experiment, if we take the "relaxed" (and incorrect) view that all numbers are just numbers, we can then say the bitwise operators behave identically for all ordinal types, provided that we regard
boolean
as a number that is one bit long.)P.s.: The only important thing to remember (from a practical point of view) is that
|
and&
withboolean
operators don't short-circuit, while||
and&&
do. I personally wouldn't overly worry about terminology too much so long as people understand what you mean. As you can't overload operators in Java (as a user) anyway, it's less critical than in languages where you can.