What is the purpose of Java's unary plus operator?

Posted by Syntactic on Stack Overflow See other posts from Stack Overflow or by Syntactic
Published on 2010-04-12T18:32:40Z Indexed on 2010/04/12 18:32 UTC
Read the original article Hit count: 299

Java's unary plus operator appears to have come over from C, via C++. As near as I can tell, it has the following effects:

  • promotes its operand to int, if it's not already an int or wider
  • unboxes its operand, if it's a wrapper object
  • complicates slightly the parsing of evil expressions containing large numbers of consecutive plus signs

It seems to me that there are better (or, at least, clearer) ways to do all of these things.

In this SO question, concerning the counterpart operator in C#, someone said that "It's there to be overloaded if you feel the need." But in Java, one cannot overload any operator. So does this operator exist in Java just because it existed in C++?

© Stack Overflow or respective owner

Related posts about java

Related posts about unary-operator