This week's book giveaway is in the Cloud/Virtualization forum. We're giving away four copies of Mastering Corda: Blockchain for Java Developers and have Jamiel Sheikh on-line! See this thread for details.
Bear Bibeault wrote:That is exactly the question that Henry addressed.
Let's turn this around -- why don't you explain why it should?
Sir, I got my answer, it was done to keep the language clean as assigning multiple meanings to single operator will lead to more programming errors. Why I got this doubt because people say that it doesn't support operator overloading, while actually it does. For instance, + is used in string concatenation as well as arithmetic operations and this in-built feature of java.
NitishK Kumar wrote:while actually it does. For instance, + is used in string concatenation as well as arithmetic operations and this in-built feature of java.
I think that's the only place where it does.
Just think of overloading the operator say "-" to do addition? So one can really not think of an use case here with operator overloading. We would be more than happy to have what the default behavior is for the operators.
The - operator is overloaded; it can be a binary operator meaning subtraction or a unary operator meaning sign change.
The + operator is overloaded; it means addition if used as a binary operator with numbers, catenation as a binary operator with Strings, and promotion as a unary operator with numbers.
The &, | and ^ operators are overloaded, for bitwise "AND" "OR" and "XOR" with (integer) numbers and logical "AND", "OR", and "XOR" with booleans.
Some people also say the ++ and -- operators are overloaded because they can be used as unary prefix and unary postfix operators. Some people also say the + - / * and % operators ought to be overloaded to take BigDecimal and BigInteger as operands.