4

There's something I really don't understand: a lot (see my comment) of people complain that Java isn't really OO because you still have access to primitive and primitive arrays. Some people go as far as saying that these should go away...

However I don't get it... Could you do efficiently things like signal processing (say write an FFT, for starters), writing efficient encryption algorithms, writing fast image manipulation libraries, etc. in Java if you hadn't access to, say, int[] and long[]?

Should I start writing my Java software by using List<Long> instead of long[]?

If the answer is "simply use higher-level libraries doing what you need" (for example, say, signal processing), then how are these libraries supposed to be written?

Gugussee
  • 1,673
  • 3
  • 16
  • 26
  • 2
    (*) Here's an example from a well-respected and famous programmer advocating that the next Java shouldn't allow primitives nor arrays of primitives: http://www.jroller.com/scolebourne/entry/the_next_big_jvm_language1 – Gugussee Mar 04 '11 at 16:11
  • some excellent responses http://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster – Brandon Frohbieter Mar 04 '11 at 16:18
  • @Orbit: as far as I can see this is totally unrelated. I'm not talking about arrays of objects versus List of objects. I'm talking about huge arrays of primitives. It's an entirely different beast: try to do signal processing using *List{Long}* vs *long[]* and you'll see my question is entirely different :) – Gugussee Mar 04 '11 at 16:24

4 Answers4

3

I personally use List most of the times, because it gives you a lot of convenience. You can also have concurrent collections, but not concurrent raw arrays.

Almost the only situation I use raw arrays is when I'm reading a large chunk of binary data, like image processing. I'm concerned instantiating e.g.Byte objects 100M times, though I have to confess I never tried working with that huge Byte list. I noticed when you have something like a 100KB file, List<Byte> works ok.

Most of the image processing examples etc. use array as well, so in this field it's actually more convenient to use raw arrays.

So in conclusion, my practical answer to this is

Use wrappers unless you are

  1. Working with a very large array like length > 10M (I'm too lazy to write a benchmark!),
  2. Working in a field where many examples or people prefer raw arrays (e.g. network programming, image processing),
  3. You found out there is a significant performance gain by changing to raw arrays, by doing experiments.
  4. If for whatever reason it's easier to work with raw arrays on that problem for you.
Enno Shioji
  • 26,542
  • 13
  • 70
  • 109
2

In high performance computing, arrays of objects (as well as primitives) are essential as they map more robustly onto the underlying CPU architecture and behave more predictably for things such as cache access and garbage collection. With such techniques, Java is being used very successfully in areas where the received wisdom is that the language is not suitable.

However, if your goal is solely to write code that is highly maintainable and provably self consistent, then the higher level constructs are the obvious way to go. In your direct comparison, the List object hides the issue of memory allocation, growing your list and so on, as well as providing (in various implementations) additional facilities such as particular access patterns like stacks or queues. The use of generics also allows you to carry out refactoring with a far greater level of confidence and the full support of your IDE and toolchain.

An enlightened developer should make the appropriate choice for the use case they are approaching. Suggesting that a language is not "OO enough" because it allows such choices would lead me to suspect that the person either doesn't trust that other developers are as smart as they are or has not had a particularly wide experience of different application domains.

AndyT
  • 1,413
  • 9
  • 11
1

It's a judgment call, really. Lists tend to play better with generic libraries and have stuff like add, contains, etc, while arrays generally are faster and have built-in language support and can be used as varargs. Select whatever you find serves your purpose better.

gustafc
  • 28,465
  • 7
  • 73
  • 99
  • same comment as the one made to Orbit: I'm not talking about arrays vs lists. I'm talking about *arrays of primitives* vs *List of wrappers*. – Gugussee Mar 04 '11 at 16:27
  • It's still a judgement call. Colebourne's problem isn't with primitives and arrays per se, but that they are "special citizens" and behave differently from objects (which causes no end of troubles when mixing them with generics, which arguably are broken, too). The thing is, in an ideal language on an ideal platform, you wouldn't need to make a distinction between a primitive and an object or an array and a list, but this is Java, and these things are sometimes needed, for interop with existing libraries, for readability, or for performance. – gustafc Mar 04 '11 at 16:34
-1

Okay.

You need to know the size of an array at the time that it is created, but you cannot change its size after it has been created. But, a list can grow dynamically after it has been created, and it has the .Add() function to do that.

Have you gone through this link ?

A nice comparison of Arrays vs List.

Community
  • 1
  • 1
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
  • totally unrelated. You just reposted as an answer something that *a)* is not an answer (see my comment to Orbit) and *b)* is the exact same link that Orbit put in comments five minutes before your post. Trying to get upvotes ? ;) – Gugussee Mar 04 '11 at 16:25
  • Oops. I didn't see that. Sorry. But I guess, the difference between is not useless. – Saurabh Gokhale Mar 04 '11 at 16:28
  • @Gugussee: I thought trying to get upvotes when answering a question was assumed? :) – James Mar 04 '11 at 16:28