• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of Arrays of Wrapper Data Types.  RSS feed

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi all

Somewhere in the project i am working on, i see the usage of Integer Arrays .
In this case length od array is fixed because we know the size has to be same and we are not using collections like List<Integer> .
framework used is Spring ,SWF ,JSP with DB2.
We have that design and DAO layer and service layer and POJOs.
Somehow i have this doubt(may be i read somewhere) to use collections as much as possible.
and not Integer arrays but since size is also fized(constant arrays) we use this and knowing using core api is always better.

Which one is performance-effective.?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop worrying about performance and start worrying about code readability. List<Integer> is much more clear than Integer[].

On top of that, List<Integer> is much more flexible as well. What is your reason to prefer arrays over collections?
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only reason for using wrapper arrays over collection -

fixed size of array and little better performance since using collections could be slower than using Integer[]
but yes Integer[] does look ugly and i intend to change it.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost always, clarity and maintainability of the code is much more important than micro-optimizations.

you should especially not "optimize" code purely based on your intuition that something is faster than something else (for example "my intuition tells me that arrays are faster than collections"). Your intuition is often wrong, or the performance difference is so small that it does not matter at all - the cost of making your code ugly but "fast" most often outweighs the benefits of making the code clear and easy to maintain.

Only optimize if you've measured that there is an actual performance problem, and optimize only based on actual evidence (measurements, for example with a profiler). Optimizing code that only your intuition tells you needs optimizing often means that you're wasting your time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!