• Post Reply Bookmark Topic Watch Topic
  • New Topic

Operator += and =+ behaviors?  RSS feed

 
Keith Alexander
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doing some exercises I used the operator += ... e.g.

gradeSum += studentSum[i]

All was well. When I accidentally had the operator the wrong way round ... e.g.

gradeSum =+ studentSum[i]

it compiled AND ran, giving me some odd results. Is there such an operator, =+, and if so, what is the published behavior?

Thanks.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's two operators. There's the assignment operator (=), which assigns the value on its right-hand side to the variable on its left-hand side. And then there's the unary addition operator (+), which has only one operand (on its right-hand side) and returns the value of that operand.

 
Keith Alexander
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, Java still sees this as two operators not a 'new' defined one, thanks. So let's see if I understand this right ... using the two operators in the wrong order below (again by mistake) ...

int i = 10;
int j = 15;

i =+ j;

I get i=15 (tests confirmed). As the '=' operator is assigning LHS to RHS value but before that '+' operator is assigning RHS to simply 'j'.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, no, that's not the "wrong" way. It's a perfectly legitimate (but rather useless) expression. It's just a lot different than the "+=" operator.

You're assigning the value of "+ j" to the variable "i". And the value of the expression "+ j" is the value of the RHS of the "+" operator, namely just "j". Nothing is assigned to the variable "j" at any time.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote: . . . there's the unary addition operator (+), which has only one operand (on its right-hand side) and returns the value of that operand.

That’s not addition, but promotion. It returns the same value as its operand, expanded to however many bits. It is rarely used, and you can read about it in the Java Language Specification. In the case of an int, which occupies 32 bits, it returns that int in 32 bits, i.e. no change.
As I said it is rarely used, and you can try it like this, expanding an 8‑bit number into 32 bits:-You will probably find the second line throws a compiler error that you are trying to fit 32 bits into the 8 bits of a byte. I am hard put to think of a circumstance where you actually need to use unary +.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I guessed there might be some of that picky stuff related to bytes versus ints thrown in there. But since I never use the unary + operator, and don't foresee any time in the future that I will ever have to use it, that stuff is something I don't need to know.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:. . . I never use the unary + operator, and don't foresee any time in the future that I will ever have to use it, . . .
So why have they got that operator in the first place?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Paul Clapham wrote:. . . I never use the unary + operator, and don't foresee any time in the future that I will ever have to use it, . . .
So why have they got that operator in the first place?


I am assuming that the main reason is because it is an artifact from the C language. As for it's purpose, like C, I guess it can also type check to confirm that the expression is a number, and implicitly cast it to int. Not sure of this, but it may be use to implicitly unbox as well.

Henry
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Henry. Best explanation I have seen for it, but it still sounds like something they found in C++ and were scared to throw out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!