Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Array initialization

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a large local array that is initialized to contain strings, what is the overhead versus perhaps having the same declaration as a final class variable?

Example:

public class stringstuff {
method_a()
{
String[] names = {"name1","name2","name3",...., "namelargenumber"};
...
}
}

I assume that at execution the local variable is allocated onto the stack but that each String is placed on the heap. Are the strings already on the heap as part of the containing class? Or is it more efficient to have:

public class stringstuff {
public final String[] names = {"name1","name2","name3",...., "namelargenumber"};
method_a()
{
...references to names[]
}
}

Where is the overhead in each case and is the difference enough to matter?




 
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say write what makes the most sense first, and don't worry about micro-optimizations like this. If you need it as a class variable, make it as a class variable, otherwise don't. If you need it not-final, don't make it final, otherwise do.

Odds are that any time you may (or may not) save would be more than lost by debugging code that is written in a way that is hard to follow.
 
Evacuate the building! Here, take this tiny ad with you:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic