What Is the Purpose of Java's Unary Plus Operator

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

The unary plus operator performs an automatic conversion to int when the type of its operand is byte, char, or short. This is called unary numeric promotion, and it enables you to do things like the following:

char c = 'c';
int i = +c;

Granted, it's of limited use. But it does have a purpose. See the specification, specifically sections §15.15.3 and §5.6.1.

What's the significant use of unary plus and minus operators?

The Unary + operator converts its operand to Number type.
The Unary - operator converts its operand to Number type, and then negates it.
(per the ECMAScript spec)

In practice, Unary - is used for simply putting negative numbers in normal expressions, e.g.:

var x = y * -2.0;

That's the unary minus operator at work. The Unary + is equivalent to the Number() constructor called as a function, as implied by the spec.

I can only speculate on the history, but the unary +/- operators behave similarly in many C-derived languages. I suspect the Number() behavior is the addition to the language here.

What is the purpose of the unary plus (+) operator in C?

As per the C90 standard in 6.3.3.3:

The result of the unary + operator is the value of its operand. The integral promotion is
performed on the operand. and the result has the promoted type.

and

The operand of the unary + or - operator shall have arithmetic type..

What does plus operator with empty operands do in Java?

Here's a hint.

As you pointed out

int a = 1;
int b = - - - - - - a;
System.out.println(b); //prints 1

But using an odd number of - signs gives

b = - - - - - a;
System.out.println(b); // prints -1

I leave it up to you to arrive at the answer.

What does the unary plus operator do?

It's there to be overloaded if you feel the need; for all predefined types it's essentially a no-op.

The practical uses of a no-op unary arithmetic operator are pretty limited, and tend to relate to the consequences of using a value in an arithmetic expression, rather than the operator itself. For example, it can be used to force widening from smaller integral types to int, or ensure that an expression's result is treated as an rvalue and therefore not compatible with a non-const reference parameter. I submit, however, that these uses are better suited to code golf than readability. :-)

What's the point of unary plus operator in Ruby?

Perhaps it's just a matter of consistency, both with other programming languages, and to mirror the unary minus.

Found support for this in The Ruby Programming Language (written by Yukihiro Matsumoto, who designed Ruby):

The unary plus is allowed, but it has no effect on numeric operands—it simply returns the value of its operand. It is provided for symmetry with unary minus, and can, of course, be redefined.

What does '+i' mean in Java?

That is the plus unary operator +. It basicaly it does numeric promotion, so "if the operand is of compile-time type byte, short, or char, it is promoted to a value of type int".

Another unary operator is the increment operator ++, which increments a value by 1. The increment operator can be applied before (prefix operator) or after (postfix operator) the operand. The difference is that the prefix operator (++i) evaluates to the incremented value, whereas the postfix operator (i++) evaluates to the original value.

int i = -1;
System.out.println(+i); // prints -1

System.out.println(i++); // prints -1, then i is incremented to 0

System.out.println(++i); // i is incremented to 1, prints 1

Why does this Java code with + + compile?

It is the unary plus, twice. It is not a prefix increment because there is a space. Java does consider whitespace under many circumstances.

The unary plus basically does nothing, it just promotes the operand.

For example, this doesn't compile, because the unary plus causes the byte to be promoted to int:

byte b = 0;
b = +b; // doesn't compile

Why doesn't the unary plus have the meaning of absolute value?

It's not implemented that way because math doesn't work that way.

In math:

note: the following is math on a blackboard, not a programming language

+ -5 = -5

- -5 = 5

Doing it any other way will confuse anyone who's ever finished highschool.

Obviously, the above is fairly weak answer since you can indeed implement the action taken by the ASCII character '+' in the code to work differently from math. In Lisp for example, the + operator does not work like regular highschool math:

;;; Adding two numbers:
+ 1 2

And on TI calculators, it's even reversed:

1 2 +

You could argue that implementing + the way you describe in your programming language makes sense. There are a number of different arguments on why it should not be done including the principle of least surprise, a language should model the problem domain, it's what we've been taught at school etc. All of them are essentially weak arguments because they don't provide a fundamental reason for a machine to treat + the same way as math.

But here's the thing, humans are emotional creatures. We like what's familiar and cringe at something that goes against something we've been taught. And programmers don't like being surprised by + behaving differently from normal. And programmers are the ones who create programming languages so it makes sense that they implement + the way they do.

One programmer may not think like that, or a small group, and they are free to create a programming language that implements + differently. But they'll have a hard time convincing the programming community at large they what they did was right.

So it doesn't matter so much that all the arguments that can be made against + acting as abs() are essentially weak. As long as the majority feels it's right + will behave the same as regular math.



Related Topics



Leave a reply



Submit