Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

i++ or ++i or i = i +1 faster ?? 40 methods doing 1 thing

843785Oct 29 2008 — edited Oct 31 2008
Sorry , it may sound a stupid question.

I don't know much about JVM to understand the operation behind this..

I have around 40 methods that increment different variables
public void incrementA() {
synchronized(this){
a ++ ; 
// ++ a ;  Possibly this is  the fasttest
// a = a +1 ;
}

}
I believe i = i +1 would definately be slower as this would be addition instruction

++i sounds faster to me as it is a preincrement operator
Step:
increment i value by 1
return i

i ++ may be slower
Step:
get value of i into temp
increment value of i by 1
return temp

May be i faltered at some places... Please correct me and guide me on this .. that which would be the faster of them all ??

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 28 2008
Added on Oct 29 2008
90 comments
844 views