• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Declaration of variables / references - What difference does it make?

 
Greenhorn
Posts: 14
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Whenever i code in Java, i had this habit of declaring variables, stacking them on top of method! In my perception i did this thinking that it improves clarity, readability.. also it was like a code beautification!

I am aware of the following

1 . Usually one would declare the variables/references in the top of method to decide its scoping..meaning they would require the variable or reference in the catch or finally block

2 . Declaring variables/references in the top of method and initializing them with Null or some values! would lead to Null exceptions if not instantiated inside subsequent parts of code

When i googled on this, It was said in some forum "that declare variable close to where you need it!" because its easy for JVM while doing deallocation!

Please see the below sample code snippet and help me to understand which is the best option in terms of coding standard / de-allocation?

Option 1:



Option 2:



Comparing option 1 and 2! what difference do you see in terms of coding standard / de-allocation?


 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally, I wouldn't be worrying much about the compiler or JVM. I'd use the first approach because I think it's far easier to read.
 
Srinath Kannan
Greenhorn
Posts: 14
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Matthew,

Thanks your comments!

All these days i was using option 2, from now i will use option 1. Other than readability, like in terms of memory allocation / performance do you see any difference? Is that going for option 1 guarantee's correct de-allocation at the right time!
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My preference is to minimize variable scope; declare them
at first use and then expand their scope only as necessary.
This provides minimum typing and maximum safety.

Jim ...
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Srinath Kannan wrote:Hi Matthew,

Thanks your comments!

All these days i was using option 2, from now i will use option 1. Other than readability, like in terms of memory allocation / performance do you see any difference? Is that going for option 1 guarantee's correct de-allocation at the right time!



There is not enough difference in memory allocation / performance for you to alter your code at all. Readability, making it easier to change the code should it need it, etc., should be your primary considerations.

rc
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic