0

I am currently in Java1 this semester, and I asked my teacher a question, that he could not really answer, because he didn't really know why. Or rather couldn't think of what purpose it would serve.

Let us say that, when setting variables a=5 and b=-4. And I wanted to know the value of a+b set to System.out.println.

I could do a + b, but I could also do a + + b, which means add a to the positive b. Although b is negative the positive variable is still negative 4.

However, if I change the equation to a - - b, it should come out to b being a positive 4.

So my question, is what is really the purpose of creating an equation for a positive variable if the positive variable, is just the variable.

Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30
Tanktastic
  • 49
  • 4
  • 3
    I have literally never seen `a + + b` before. I thought it would be invalid, but is apparently valid. So what's the purpose of allowing it? Perhaps in some cases it might make the intent of the code clearer. I can't think of one where it would, though. – Andy Turner Sep 11 '17 at 19:51
  • @zero298 It's the ["unary plus operator"](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.15.3) – Andy Turner Sep 11 '17 at 19:53
  • As i stated, only for compatibility, but you downvoted, so good lucky – Marcos Vasconcelos Sep 11 '17 at 21:08

1 Answers1

-1

Since the opposite is valid (adding to a negative such a + - b) then the sign plus doesnt affects its value.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167