Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java source code for ++  RSS feed

 
vicky Aggarwal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

how can i see java implementation for post increment operator ++.
Does it uses native methos for this.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Download the JVM code from the Oracle website. I think it is available under "other downlaods" or similar, but I have never tried.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vicky Aggarwal wrote:Hi,

how can i see java implementation for post increment operator ++.
Does it uses native methos for this.


There's no specific native implementation for either the preincrement or postincrement ++ operator. The java compiler expresses them in terms of primitive load/store and add operations. This Java source:



compiles into the following bytecodes (I added the comments.) You can disassemble any classfile this way using the "javap" tool:



The real difference between the two is when we push the value of "i" onto the stack . In the postincrement case, we do it before the increment; in the preincrement case, we do it after the increment. The value we push onto the stack is the one that gets copied to "j".
 
vicky Aggarwal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Ernest Friedman-Hill ..
it gives me a better understandability.
Just confuse on a single ponit....
2: iinc 1, 1 ; ADD 1 TO LV 1


here inc method directly increment the LV ... there is no need to store it again.
Store is require in case of assignment only???
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "store" bytecodes copy a value from the JVM stack into a local variable, while load copies data from a variable onto the stack. Many bytecodes operate only on values from the stack, so this is a common operation. But because iinc operates directly on the local variable, there's no need to do any load or store calls specifically related to this operation.

In "foo()", note that we keep the old value of the variable on the stack at line 2; then we increment the variable directly; then we take that stored old value from the stack at line 6 and move it into the other variable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!