Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3353736
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:11:14+00:00 2026-05-18T02:11:14+00:00

All, While going through some of the files in Java API, I noticed many

  • 0

All,

While going through some of the files in Java API, I noticed many instances where the looping counter is being decremented rather than increment. i.e. in for and while loops in String class. Though this might be trivial, is there any significance for decrementing the counter rather than increment?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:11:15+00:00Added an answer on May 18, 2026 at 2:11 am

    I’ve compiled two simple loops with eclipse 3.6 (java 6) and looked at the byte code whether we have some differences. Here’s the code:

    for(int i = 2; i >= 0; i--){}
    for(int i = 0; i <= 2; i++){}
    

    And this is the bytecode:

    // 1st for loop - decrement 2 -> 0
     0 iconst_2
     1 istore_1      // i:=2
     2 goto 8
     5 inc 1 -1      // i+=(-1)
     8 iload_1
     9 ifge 5        // if (i >= 0) goto 5
    
    // 2nd for loop - increment 0 -> 2
    12 iconst_0 
    13 istore_1      // i:=0
    14 goto 20
    17 inc 1 1       // i+=1
    20 iload_1
    21 iconst 2
    22 if_icmple 17  // if (i <= 2) goto 17
    

    The increment/decrement operation should make no difference, it’s either +1 or +(-1). The main difference in this typical(!) example is that in the first example we compare to 0 (ifge i), in the second we compare to a value (if_icmple i 2). And the comaprision is done in each iteration. So if there is any (slight) performance gain, I think it’s because it’s less costly to compare with 0 then to compare with other values. So I guess it’s not incrementing/decrementing that makes the difference but the stop criteria.

    So if you’re in need to do some micro-optimization on source code level, try to write your loops in a way that you compare with zero, otherwise keep it as readable as possible (and incrementing is much easier to understand):

     for (int i =  0; i <= 2; i++) {}  // readable
     for (int i = -2; i <= 0; i++) {}  // micro-optimized and "faster" (hopefully)
    

    Addition

    Yesterday I did a very basic test – just created a 2000×2000 array and populated the cells based on calculations with the cell indices, once counting from 0->1999 for both rows and cells, another time backwards from 1999->0. I wasn’t surprised that both scenarios had a similiar performance (185..210 ms on my machine).

    So yes, there is a difference on byte code level (eclipse 3.6) but, hey, we’re in 2010 now, it doesn’t seem to make a significant difference nowadays. So again, and using Stephens words, “don’t waste your time” with this kind of optimization. Keep the code readable and understandable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While performing some upcoming maintenance, I'm going to have to redirect all site traffic
All my questions in this topic related to asp.net 2.0 While working on some
while all tests complete without errors and fixtures are actually loaded. Is it Django
I have been using CodeIgniter all these while, and have solid foundation in OOP
I've been drooling over Django all day while coding up an internal website in
Seems I've outdone myself. All the while I was creating this pretty little 'latest
My idea was to copy a dictionary while resetting all the values of the
The ? wildcard represents unauthenticated users while * represents all users, authenticated and unauthenticated.
I'm starting to get into Unit Testing, Dependancy Injection and all that jazz while
I would like to reuse certain packages installed systemwide, while ignoring all others. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.