• Post Reply Bookmark Topic Watch Topic
  • New Topic

increment operator  RSS feed

 
sachin r pandey
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

{
int a=6;
a*=++a;
system.out.println(a);
}

output
42

can somone explain how the increment operator works here???
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Increment, decrement operators have low precedence.

++a means increment value of a before it is used.

a++ means increment value of a after it is used

Read this tutorial
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you break it down, like you do with everything else in a computer program

the *= operator means you take the value of what's on the left, multiply it by the value of whats on the right, and stick the result back into the variable on the left.

the value on the left is 6. The value on the right is computed...a is 6, the increment operator says to make it 7. so, 6 * 7 = 42.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:he value on the right is computed...a is 6, the increment operator says to make it 7...

@sachin: All the above good advice notwithstanding, your example is a classic case of how NOT to program.

It's the sort of thing you might see in a tutorial or book, just to get you understanding how operators work; but I would personally shoot any programmer that presented a program to me with that actually in it.

PS: I'm known for being tough .

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!