• Post Reply Bookmark Topic Watch Topic
  • New Topic

Varibale declaration inside or outside a loop

 
jitheshg@gmail.com
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i am a beginner in Java programming. Some people told me that its better to declare an Object outside the loop than declaring it inside. Please have a look at the attached code snippets,



Can anyone tell me which one of these is better? Is there any difference in performance or memory considering these two (suppose the iteration goes for a thousand range loop)?
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I would be inclined to believe that either of the snippets posted would not make a huge impact on performance. If the variable were declared outside the loop the difference would be that the reference would be active even after the loop is executed hence making the same eligible for garbage collection only until later. In either case you are creating instances of the object from inside the loop and hence would require the use of memory space for holding them. Unlike its predecessors Java has an efficient garbage collection system which would reclaim unused references/objects. Does this answer your query?

Cheers,
Raj.
 
jitheshg@gmail.com
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea.. thanks Raj. I appreciate this quick response.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy jitheshg!

Welcome to javaranch!
We being a very friendly community, do not have much rules but there is a naming policy that we are very strict about!
Unfortunately, your display name does not follow the policy. Please change the same according to our policy.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can anyone tell me which one of these is better? Is there any difference in performance or memory considering these two (suppose the iteration goes for a thousand range loop)?


Not really as raj said, but it is considered as a good coding practice to limit the scope of variable as small as possible.
If you are just using the variable inside the loop then it is better to declare it inside.
I am not saying that it will add/remove anything from performance but just one of the coding guidelines that is good to follow.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35728
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jitheshg,
In addition to violating our naming policy, your display name is currently inviting boatloads of spam. Having it clear text allows spiders to get it. Whereas a human interested in your e-mail would view your profile (which has it as non clear text).

Discussing which is better or worse performance wise is "premature optimization." Regardless of which is faster, they are so close that you would never see a noticeable performance difference.

Interestingly, the for loop that you used does declare the object with loop scope. Which means that performance is so good that Sun decided to make the construct a language standard. I think that makes it ok for your other variable.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The declaration of a local variable is *free*, performance wise.
 
Tim Holloway
Bartender
Posts: 18415
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't try to anticipate the optimizer. You'll end up outsmarting yourself. There are multiple layers of manipulation between what you code and what gets generated, and modern-day compilers will do significant restructuring.

More important is what the scope of the variable is in terms of the application itself. I prefer to keep scope minimize for the very simple reason that what's in scope can't be accidentally modified. For the same reason, I tend to be fairly heavy with the "final" attribute, even though I expect the compiler will generally detect access usage anyway. It's protecting me from myself. And whatever ignorant fool comes along and mucks with my code.

There are times when outside-the-box definitions are definitely preferable. One of the most common for me is when I'm declaring a resource that may be constructed within a try/catch block and needs to be safely cleaned up in a "finally" section. In that case, I have to declare the variable outside the try/catch/finally scope, since the "try" scope is lost when the "catch" and "finally" clauses are executed.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim Holloway:
Don't try to anticipate the optimizer.


I agree in general.

In this specific case, though, it's quite obvious: local variables reside on the stack, and the stack frame of a method gets reserved when the method is entered. So, it really doesn't matter where a local variable gets declared - the byte code likely already won't be different.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitesh i am seeing your reply in every question and mentioning about forum rules, you make your presence unnecessarily .

The CEO of the site should take an action and make it more helpful to the people .
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RaviNada Kiran wrote:Nitesh i am seeing your reply in every question and mentioning about forum rules, you make your presence unnecessarily .

Nitesh is a Bartender, a moderator of the site, and thus is a proper authority to monitor who is or is not conforming to the rules.

The CEO of the site should take an action and make it more helpful to the people .

You obviously haven't spent much time on many of the other Java programming fora where it's almost "anything goes". This place is as nice to newbies as any I've seen, probably nicer.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer this thread
http://www.coderanch.com/t/428206/Java-General-beginner/java/What-dfference-between-these-two
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
10x , really helpful
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You people are matured and professionals.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!