1

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:

  1. Can I say that operators & ^ | are overloaded?

  2. Is my jargon correct on calling these operators as bitwise operators on integral operands and logical operators on boolean operands?

4
  • 7
    Where did you read that + is the only overloaded operator? All the arithmetic ones are overloaded, to start with... Commented Jun 12, 2015 at 9:46
  • 5
    @shekharsuman: No, Java doesn't have user-defined operator overloading. Big difference. Commented Jun 12, 2015 at 9:46
  • 3
    @shekharsuman Java doesn't have programmable operator overloading. But the + operator is indeed overloaded. Commented Jun 12, 2015 at 9:46
  • @JonSkeet-I meant that only. Sorry :) Commented Jun 12, 2015 at 9:47

1 Answer 1

3

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 to Operators.plus(Object first, Object second), whereas an integer addition would be covered by Operators.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 & with boolean 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.

Sign up to request clarification or add additional context in comments.

5 Comments

boolean type is not integral type in java unlike C.
am not getting an undestanding of, when to call an operator as overloaded. that is my first question. BTW, I did not -1.
@overexchange I didn't say it was.
@overexchange That's simple: when the code implementing the operator is different. 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.
Actually, we would need an entire zoo of overloaded plus methods, to model all the existing combinations, or assume a behavior as-if autoboxing happens, but even then, we have (String,Object) and (Object,String) (but not (Object,Object))…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.