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 800635
In Process

The Archive Base Latest Questions

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

This isn’t meant to be subjective, I am looking for reasons based on resource

  • 0

This isn’t meant to be subjective, I am looking for reasons based on resource utilisation, compiler performance, GC performance etc. rather than elegance. Oh, and the position of brackets doesn’t count, so no stylistic comments please.

Take the following loop;

Integer total = new Integer(0);
Integer i;
for (String str : string_list)
{
    i = Integer.parse(str);
    total += i;
}

versus…

Integer total = 0;
for (String str : string_list)
{
    Integer i = Integer.parse(str);
    total += i;
}

In the first one i is function scoped whereas in the second it is scoped in the loop. I have always thought (believed) that the first one would be more efficient because it just references an existing variable already allocated on the stack, whereas the second one would be pushing and popping i each iteration of the loop.

There are quite a lot of other cases where I tend to scope variables more broadly than perhaps necessary so I thought I would ask here to clear up a gap in my knowledge. Also notice that assignment of the variable on initialisation either involving the new operator or not. Do any of these sorts of semi-stylistic semi-optimisations make any difference at all?

  • 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-14T23:19:11+00:00Added an answer on May 14, 2026 at 11:19 pm

    The second one is what I would prefer. There is no functional difference other than the scoping.

    Setting the same variable in each iteration makes no difference because Integer is an immutable class. Now, if you were modifying an object instead of creating a new one each time, then there would be a difference.

    And as a side note, in this code you should be using int and Integer.parseInt() rather than Integer and Integer.parse(). You’re introducing quite a bit of unnecessary boxing and unboxing.


    Edit: It’s been a while since I mucked around in bytecode, so I thought I’d get my hands dirty again.

    Here’s the test class I compiled:

    class ScopeTest {
        public void outside(String[] args) {
            Integer total = 0; 
            Integer i; 
            for (String str : args) 
            { 
                i = Integer.valueOf(str); 
                total += i; 
            }
        }
        public void inside(String[] args) { 
            Integer total = 0; 
            for (String str : args) 
            { 
                Integer i = Integer.valueOf(str); 
                total += i; 
            }
        }
    }
    

    Bytecode output (retrieved with javap -c ScopeTest after compiling):

    Compiled from "ScopeTest.java"
    class ScopeTest extends java.lang.Object{
    ScopeTest();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
    
    public void outside(java.lang.String[]);
      Code:
       0:   iconst_0
       1:   invokestatic    #2; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       4:   astore_2
       5:   aload_1
       6:   astore  4
       8:   aload   4
       10:  arraylength
       11:  istore  5
       13:  iconst_0
       14:  istore  6
       16:  iload   6
       18:  iload   5
       20:  if_icmpge       55
       23:  aload   4
       25:  iload   6
       27:  aaload
       28:  astore  7
       30:  aload   7
       32:  invokestatic    #3; //Method java/lang/Integer.valueOf:(Ljava/lang/String;)Ljava/lang/Integer;
       35:  astore_3
       36:  aload_2
       37:  invokevirtual   #4; //Method java/lang/Integer.intValue:()I
       40:  aload_3
       41:  invokevirtual   #4; //Method java/lang/Integer.intValue:()I
       44:  iadd
       45:  invokestatic    #2; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       48:  astore_2
       49:  iinc    6, 1
       52:  goto    16
       55:  return
    
    public void inside(java.lang.String[]);
      Code:
       0:   iconst_0
       1:   invokestatic    #2; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       4:   astore_2
       5:   aload_1
       6:   astore_3
       7:   aload_3
       8:   arraylength
       9:   istore  4
       11:  iconst_0
       12:  istore  5
       14:  iload   5
       16:  iload   4
       18:  if_icmpge       54
       21:  aload_3
       22:  iload   5
       24:  aaload
       25:  astore  6
       27:  aload   6
       29:  invokestatic    #3; //Method java/lang/Integer.valueOf:(Ljava/lang/String;)Ljava/lang/Integer;
       32:  astore  7
       34:  aload_2
       35:  invokevirtual   #4; //Method java/lang/Integer.intValue:()I
       38:  aload   7
       40:  invokevirtual   #4; //Method java/lang/Integer.intValue:()I
       43:  iadd
       44:  invokestatic    #2; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       47:  astore_2
       48:  iinc    5, 1
       51:  goto    14
       54:  return
    
    }
    

    Contrary to my expectations, there was one difference between the two: in outside(), the variable i still took up a register even though it was omitted from the actual code (note that all the iload and istore instructions point one register higher).

    The JIT compiler should make short work of this difference, but still you can see that limiting scope is a good practice.

    (And with regards to my earlier side note, you can see that to add two Integer objects, Java must unbox both with intValue, add them, and then create a new Integer with valueOf. Don’t do this unless absolutely necessary, because it’s senseless and slower.)

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

Sidebar

Related Questions

This isn't a matter of me being stuck, but rather I'm looking for a
This isn't meant as a troll or flamebait or anything like that. I've been
(this isn't duplicate of Changing Django settings variable dynamically based on request for multiple
This isn't really clearly documented, but a shallow search reveals that RIM's RAPC compiler
This isn't a development question, I'm Looking for a plugin for Joomla that allows
This isn't necessarily a programming question, but I've hit a performance bottleneck with disk
This isn’t a specific question with a specific solution; but it’s rather a response
This isn't about a side-by-side technical comparison, rather about how to think in jQuery
This isn't actually homework, I'm just looking through some questions in a discrete maths
This isn't the best question ever, but since search engines feel the need to

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.