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

7

44

Java's unary plus operator appears to have come over from C, via C++.

int result = +1;   

It appears to have the following effects:

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

It seems to me that there are better/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."

However, in Java, one cannot overload any operator. So does this unary plus operator exist in Java only because it existed in C++?

Tecumseh answered 12/4, 2010 at 18:32 Comment(2)
Code obfuscation contests... the SCJP exam...Torey
Similar question for C, which does not have overload either: #6637505 , and analogously to Java does type promotion.Solleret
O
50

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.

Officer answered 12/4, 2010 at 18:52 Comment(9)
Right, but it's redundant in that case. Since that's a widening conversion, int i = c; will do the same thing.Tecumseh
It's not necessarily redundant in a holistic sense, because it conveys intention. One might legitimately wonder of int i = c, "did the author of this code mean to assign c to an int?" Are there other ways to do it? Sure, but this is the shortest way that also conveys intention.Officer
Good point. I think in practice I'd be more likely to do int i = (int) c; if I wanted to convey intention, but using the + is certainly shorter.Tecumseh
@JohnFeminella "conveys intention"? I don't really buy that since others reading the code wouldn't really know why it's there. As you can see from the other answers, it seems most people have no idea about unary numeric promotion. Personally, I always thought it was a no-op and it is there for purely cosmetic reasons. If numeric promotion is your intent, then (int) c is much clearer since it is a ubiquitous, widely accepted pattern. Still, +1 for teaching me something I didn't know.Velar
I agree that (int) c is clearer. But I didn't say this was the clearest way to do the conversion and also convey intention; I said it was the shortest. :)Officer
but does int a = -1; b = +a result in b = 1?Burdock
@Burdock No, it doesn't.Containment
I use unary + to enable vertical coherence in complex expressions.Trudytrue
The clearest way is without a cast as it is the simplest case of numeric promotion.Megass
L
13

Here's a short demonstration of what the unary plus will do to a Character variable:

private static void method(int i){
    System.out.println("int: " + i);
}

private static void method(char c){
    System.out.println("char: " + c);
}

public static void main(String[] args) {
    Character ch = 'X';
    method(ch);
    method(+ch);        
}

The output of running this programme is:

char: X
int: 88

How it works: Unary + or - unbox their operand, if it's a wrapper object, then promote their operand to int, if not already an int or wider. So, as we can see, while the first call to method will choose the char overload (unboxing only), the second call will choose the int version of method. Variable ch of type Character will be passed into method as int argument because of the applied unary plus.

Lynnell answered 15/4, 2013 at 12:43 Comment(1)
What exactly is a "programme"?Alkyd
R
6

I don't know, but I suspect it's there for symmetry with the (obviously necessary) unary minus operator.

Rotow answered 12/4, 2010 at 18:35 Comment(2)
It's entirely possible that if there weren't a unary plus operator in Java, I'd be sitting here on Stack Overflow complaining about it...Tecumseh
@Tecumseh Unlikely. If there were no unary plus operator, you could use the intValue() method instead.Alkyd
D
3

One of Java's design goals was to be familiar (to a C/C++ developer), so when it came to operators like this I'm pretty sure they would have to have a strong reason to exclude it, not a good reason to need it.

Dingus answered 12/4, 2010 at 19:50 Comment(0)
T
2

My guess is it's there because sometimes typing the plus out makes things clearer. You might want to emphasize the fact that some number is positive, as opposed to some negative number.

Also, to provide a real world example where it's used, positive temperatures tend to be always prefixed with a plus in some parts of the world.

Teleview answered 12/4, 2010 at 18:50 Comment(0)
N
2

Many other languages have unary plus. It's customary to include it, and penny-pinching to exclude it. It's only a couple of lines in a compiler.

Northeastwards answered 13/4, 2010 at 7:40 Comment(0)
A
-3

The unary operators do the following for java:

  • + Unary plus operator; indicates positive value (numbers are positive without this, however)
  • - Unary minus operator; negates an expression
  • ++ Increment operator; increments a value by 1
  • -- Decrement operator; decrements a value by 1
  • ! Logical complement operator; inverts the value of a boolean

Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

Avow answered 28/9, 2016 at 1:32 Comment(1)
The link you provided is misleading. Unary + does not "indicate a positive value": +-1 equals -1.Alkyd

© 2022 - 2024 — McMap. All rights reserved.