43

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++?

Community
  • 1
  • 1
Syntactic
  • 10,721
  • 3
  • 25
  • 25

7 Answers7

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • 28
    Right, but it's redundant in that case. Since that's a widening conversion, `int i = c;` will do the same thing. – Syntactic Apr 12 '10 at 18:56
  • 9
    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. – John Feminella Apr 12 '10 at 19:35
  • 15
    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. – Syntactic Apr 12 '10 at 23:26
  • 12
    @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. – Nicu Stiurca Aug 05 '12 at 00:15
  • 4
    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_. :) – John Feminella Aug 05 '12 at 04:48
  • but does `int a = -1; b = +a` result in `b = 1`? – CpILL May 23 '15 at 11:41
  • @CpILL No, it doesn't. – Radiodef May 23 '15 at 17:13
  • 1
    I use unary + to enable vertical coherence in complex expressions. – vwvan Apr 12 '16 at 22:30
  • The clearest way is without a cast as it is the simplest case of numeric promotion. – downvoteit Nov 01 '21 at 13:38
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.

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
Peter Perháč
  • 20,434
  • 21
  • 120
  • 152
6

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

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
  • 11
    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... – Syntactic Apr 12 '10 at 19:08
  • @Syntactic Unlikely. If there were no unary plus operator, you could use the `intValue()` method instead. – Sapphire_Brick Jan 20 '21 at 22:02
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.

Yishai
  • 90,445
  • 31
  • 189
  • 263
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.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
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.

user207421
  • 305,947
  • 44
  • 307
  • 483
-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

dan1st
  • 12,568
  • 8
  • 34
  • 67