programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# The += operator.

Keano Denton
Greenhorn
Posts: 13
I'm confused on how this one works. Does this operator add to the left until it is equal to the right?

So if i write-

int x = 0;
int y = 1;

x += y;

The JVM will add to x until it is equal to y?

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
No. x+=y is a shortcut for x = x + y.

Which means add x+y (0+1) which gives you 1 and assign it to x. At the end of x+=y, x = 1 and y=1. They happen to be equal in your case.

As another example
int x = 5;
int y = 3;
x += y;

Now x = 8 and y = 3

Keano Denton
Greenhorn
Posts: 13
Ahhh!! I understand now. Thank you very much. That really helps, and also shows how much of a greenhorn I am.

Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
Everyone was a greenhorn at one time.

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Keano Denton wrote:The JVM will add to x until it is equal to y?

That would make no sense, you could just as well do x = y if you wanted x to become equal to y.

Keano Denton
Greenhorn
Posts: 13
• 1
Well Jesper,

That would be the reason for me asking the question. Because if you didn't read my title or the multiple posts in this topic where I acknowledge that I am a very big greenhorn. I was confused and unsure of the actual use of that operator, and in my last post I thanked Jeanne for explaining it to me. So at the time I posted my first question I did not know that it didn't make any sense, and now I do thanks to Jeanne. Have a great day.

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Keano Denton wrote: and now I do

That means the site is working .

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
• 1
Keano Denton wrote:That would be the reason for me asking the question. Because if you didn't read my title or the multiple posts in this topic where I acknowledge that I am a very big greenhorn. I was confused and unsure of the actual use of that operator, and in my last post I thanked Jeanne for explaining it to me. So at the time I posted my first question I did not know that it didn't make any sense, and now I do thanks to Jeanne. Have a great day.

Hi Keano,

Remember that our mission here isn't to GIVE you the answer...it's to help you learn how to find answers on your own. We love it when greenhorn's ask questions. But often we're going to use a sort of Socratic method...we're going to ask you questions to try and lead you to the answer.

So Jesper's comment was really an effort to get you to think about things a little differently so that next time you have a question, you may pause, think about it for a bit, and realize the answer yourself.

Again, it's not that we don't want you to ask questions here...but we'd much rather you learn how to think through issues and come up with the answer yourself - that is a sign of a good, smart developer.