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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:17:32+00:00 2026-06-04T17:17:32+00:00

With regard to Memory usage and variable instantiation which is better or is there

  • 0

With regard to Memory usage and variable instantiation which is better or is there no difference :

This

for(int i = 0; i < someValue; i++)
{
    Obj foo = new Obj();
    Use foo.....
}

As opposed to:

Obj foo;

for(int i = 0; i < someValue; i++)
{
    foo = new Obj();
    Use foo.....
}
  • 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-06-04T17:17:33+00:00Added an answer on June 4, 2026 at 5:17 pm

    There is no difference. Any potential difference in terms of memory usage would be optimized by the compiler.

    If you compile (using javap -c) the two examples and compare the bytecode, you’ll find that the bytecode is the same. Of course, this depends on the JVM version. But since this example is so trivial, it’s probably safe to assume that neither is more memory efficient than the other.


    Example 1:

    Code:

    public class example1 {
        public static void main(String[] args) {
            for (int i=0; i<10; i++) {
                Object a = new Object();
            }
        }
    }
    

    Bytecode:

    public class example1 extends java.lang.Object{
    public example1();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
    
    public static void main(java.lang.String[]);
      Code:
       0:   iconst_0
       1:   istore_1
       2:   iload_1
       3:   bipush  10
       5:   if_icmpge       22
       8:   new     #2; //class java/lang/Object
       11:  dup
       12:  invokespecial   #1; //Method java/lang/Object."<init>":()V
       15:  astore_2
       16:  iinc    1, 1
       19:  goto    2
       22:  return
    }
    

    Example 2:

    Code:

    public class example2 {
        public static void main(String[] args) {
            Object a;
            for (int i=0; i<10; i++) {
                a = new Object();
            }
        }
    }
    

    Bytecode:

    public class example2 extends java.lang.Object{
    public example2();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
    
    public static void main(java.lang.String[]);
      Code:
       0:   iconst_0
       1:   istore_2
       2:   iload_2
       3:   bipush  10
       5:   if_icmpge       22
       8:   new     #2; //class java/lang/Object
       11:  dup
       12:  invokespecial   #1; //Method java/lang/Object."<init>":()V
       15:  astore_1
       16:  iinc    2, 1
       19:  goto    2
       22:  return
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to see the memory usage of NIO buffers? There is
With regards to SQL and queries, whats the difference between an in-memory table, temp
This is in regard to the post https://stackoverflow.com/a/4171484/1220659 I want to know where and
I have few question in this regard When you create an internet page, does
I am newbie with Selenium Grid therefore any help in this regard is much
What are some good tips for keeping memory usage low in a Perl script?
I don't have much experience with QT and this problem came out today. QList<int>
I have an 32bit application with very large memory requirements. I noticed that there
I have troubles finding a memory leak which then again causes my App to
I am new to C, and this struct, representing an image, is confusing 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.