?

Log in

No account? Create an account
Java 1.5 - Nick [entries|archive|friends|userinfo]
Nick

[ website | gagravarr.org ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Java 1.5 [Dec. 4th, 2005|05:31 pm]
Nick
For our big project, we decided to move to Java 1.5. What follows are my thoughts on the new features (well, those new features we used).

Generic Types - for us, these were probably the biggest new function. (If you don't know about this, you can type things that take an Object. So, instead of "Collection foo = getAllShapes(); Shape first = (Shape)foo.iterator.next();", you do "Collection foo = getAllShapes(); Shape first = foo.iterator.next();") We really liked the lack of casting everywhere, and we felt a lot more confident passing collections etc of similar things about, since we could be sure we were dealing with the right things.

My one complaint with them is with interfaces and subclasses. Say you have an interface "ShapeGenerator", that defines a method "Collection getAll()". We then have a class "Square", which extends Shape. Then, we have SquareGenerator, which extends ShapeGenerator. You would expect that SquareGenerator could return "Collection" from its getAll() without issue. Annoyingly though, you can't, and end up having to cast it explicitly so it'll be happy.

Enums - quite a cool concept, but a bit fiddly to use. Having a defined, limited list of states etc is a really nice idea. However, we found that doing something as simple as supplying a nice toString() method was a lot of faff (you end up defining toString in the Enum definition, then again against each Enum). Also, they're a pain to work with in JSTL - we ended up having to do a JSTL choose (like a switch) against the toString'd values!

foreach for loops - these made code a lot nicer to read. Why it took so long to be able to run a for loop against an iterator etc I don't know. Now if only they had a secret integer counter thrown in...


A couple of things about Java that annoy me haven't been fixed though. Multiple inheritance still isn't there, so we ended up with a pile of static helper classes, which wasn't nearly so clean. Interfaces still can't define static methods or constructors, both of which limit their usefulness for enforcing uniform behaviour and contracts, and mean you need nasty cludges for that functionality. Ditto "abstract static" in abstract classes. Oh, and inner classes implementing interfaces for callbacks still don't allow as much flexibility as I'd.
linkReply