Atom Feed SITE FEED   ADD TO GOOGLE READER

An idea: auto-unboxing

Primitive Collections
I'm aware of at least two libraries for Java that provide collections for each of the six primitive types. So instead of storing Integer Objects in an ArrayList, you get to store int primitives in an IntArrayList. This saves RAM and it speeds things up. It's a clever optimization.

But this optimization is not very Java-ish. Where's the reusable in reusable collections when we need seven of them? It is a terrible violation of the Don't Repeat Yourself mantra.

Autoboxing
Java 5 introduced a new feature for using primitive types with old-school Object collections. It's called autoboxing and basically it allows you to replace this:
    List myList = new ArrayList();
myList.add(new Integer(867));

with this:
    List myList = new ArrayList();
myList.add(867);

This is simple syntactic sugar. The compiler implicitly adds the new Integer() for you.

Idea: Auto-unboxing
What if Java supported primitives with generics? This could be implemented in the compiler: ArrayList<T> could be converted into all of List<int>, List<short> etc. This would require seven .class files, which is already necessary for the other primitive collections implementations. But it would require only a single .java file.

Some problems
Collections classes use some methods only found in Object.class such as hashCode, equals() etc. This would need fixing with some unnatural hackery. The compiler would also need to know when to compile such a primitive generic class.
IIRCthis is what generics in c# (will) do.
Other languages do not have primitive tipes separated from Objects, so this is a no-brainer.

But, AFAIK, the idea to keep in mind is that java generics are backward compatible, so you can't add a feature like this since it would break old software.