• Post Reply Bookmark Topic Watch Topic
  • New Topic

diff between += and =+ different output

 
marco marco
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class desk {
public static void main(String args[]) {
int b = 1;
int a =0;
for(int i=0; i< 10; i++){
a =+ b;
// a += b;
}
}
}

Running a =+ b; and then a += b; gives you two different answers.

I know that a =+ b Equivalent to a = a + b

what is a +=b equivalent to ?

Why is that ?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that what's happening here is the following

a =+ b is not an increment operator.

I believe it is like saying a = (+b).

So we aren't incrementing a.
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a good practice to start your names with uppercase i.e. "Desk" not "desk".

a += b means a = a+b

but a =+ b means a = b;
and a =- b means a = -b;

 
marco marco
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great. Thank you for your heplp
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12056
232
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bit late, but moving this to JiG (Beginners).
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!