| Author |
Use of Arrays of Wrapper Data Types.
|
Lucky J Verma
Ranch Hand
Joined: Apr 11, 2007
Posts: 277
|
|
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
Bartender
Joined: Sep 20, 2010
Posts: 3047
|
|
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
Joined: Apr 11, 2007
Posts: 277
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: Use of Arrays of Wrapper Data Types.
|
|
|