Log in

No account? Create an account
Java! - Greg [entries|archive|friends|userinfo]

[ website | gregstoll.com ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

[Links:| * Homepage * Mobile apps (Windows Phone, Win8, Android, webOS) * Pictures * LJBackup * Same-sex marriage map * iTunesAnalysis * Where's lunch? ]

Java! [Jun. 15th, 2006|10:25 am]
[Tags|, ]
[Current Mood |mellowmellow]
[Current Music |The Killers - "Mr. Brightside"]

I was feeling a little blah yesterday, so I did what usually clears that up for me - start a new project! (this probably isn't a great long-term solution, but it works for now) I decided to write a program that would turn my iTunes playlists (which are stored in the iTunes XML file) into plain ol' .m3us so I could listen to them served up from my computer. Not too tricky, since I already have an analysis of my songs in iTunes that parses the iTunes XML file.

To make it more exciting, I started writing it in Java, which I used extensively at Rice, but hardly at all since then. I was going to parse the XML file and then use XPath to get the stuff I needed, but the file is so big (200000+ lines!) that Java runs out of memory getting the nodes. Sigh. So I went back to plain ol' parsing it line by line and just picking out the parts I need, which turned out to be pretty easy.

It's not done yet, but I've already extracted the playlists and which songs they refer to (by referencing the database that is created when I do the analysis of my songs), so I just need to write the code to output them as .m3us which is pretty straightforward. But the point of this post is to point out some new stuff in JDK 5 (which is really 1.5) that are very nice compared with what we had back at Rice :-) The link describes them, but I'll point out the highlights:

  • Generics! This means you can have a Vector<String> instead of just a Vector, and then having to cast everything you get out from there to a String. And it provides type-safety, which is always a plus. We actually used a version of this in COMP 311 (the JSR 14 that led to this being put in JDK 5), but it's nice to see it's "for real" now.

  • Autoboxing/unboxing! Awww yeah. This is great - now instead of constantly converting between an Integer and an int, the compiler will do most of the conversions for you. This had been a major annoyance because int doesn't extend from Object, so if you want a Vector of integers you'd have to store them as Integers, and adding one to an element would look like this:

    Vector v;
    v.set(2, new Integer((Integer)v.get(2)).intValue() + 1));

    Now you can do this!

    Vector<Integer> v;
    v.set(2, v.get(2) + 1);

    which really warms my heart. Plus it was one of the things that students in COMP 212 would often forget to do and then (reasonably) get confused about why this was necessary. Really the right thing to do would be something like .NET does - have a ValueType that primitive types extend from - it's stack-allocated and doesn't contain any extra data members, but it is a "full" type and extends from Object. You still have to do some boxing and unboxing, but then at least you could have a Vector. (but I'm pretty sure it's too late to do something like that for Java, and this is a good compromise)

  • Enumerations! So now you can have a real enum like
    enum Season { SPRING, SUMMER, WINTER, FALL }
    instead of declaring a class that has those as public final static int members, and you get type safety and some other nice things, much like .NET has.

  • Neat for loop syntax so you can do
    for (Integer i: v)
    to iterate over all elements of a vector, for example. To do this the old way you had to something like
    for (Iterator it = v.iterator(); it.hasNext(); it++)
    and then get the element with it.next(). It's not a huge deal, but that idiom was extremely common, and having a shorter syntax is always nice!

So, anyway those made programming in Java a bit less painful, but it's still awkward going from a dynamically-typed language (Ruby, Python) to a statically-typed one (Java). Just takes some getting used to, is all.

On a random note, I rearranged my home page a bit, hopefully for better clarity and for putting the most important/interesting stuff towards the top.

[User Picture]From: omega697
2006-06-15 04:27 pm (UTC)
Technically, Generics don't come with type safety, AFAIK. The compiler just makes sure that all the things in angle brackets are the same throughout. Speaking of which, LJ suppressed your angle brackets above.
(Reply) (Thread)
[User Picture]From: gregstoll
2006-06-15 04:31 pm (UTC)
Well, it means you don't have to do unsafe casts when you get stuff out of the vector, for instance. And I'm pretty sure you'll get a compile-time error if you try to insert something that isn't an Integer into a Vector<Integer> (based on the discussion of generics), so that seems like type safety to me.

Ah, thanks! Should have seen that coming, but it's fixed now.
(Reply) (Parent) (Thread)
[User Picture]From: omega697
2006-06-15 04:41 pm (UTC)
It's close, but not quite.

"Generic types (or generics) bear a superficial resemblance to templates in C++, both in their syntax and in their expected use cases (such as container classes). But the similarity is only skin-deep -- generics in the Java language are implemented almost entirely in the compiler, which performs type checking and type inference, and then generates ordinary, non-generic bytecodes. This implementation technique, called erasure (where the compiler uses the generic type information to ensure type safety, but then erases it before generating the bytecode), has some surprising, and sometimes confusing, consequences. While generics are a big step forward for type safety in Java classes, learning to use generics will almost certainly provide some opportunity for head-scratching (and sometimes cursing) along the way"

From http://www-128.ibm.com/developerworks/java/library/j-jtp01255.html
(Reply) (Parent) (Thread)
[User Picture]From: gregstoll
2006-06-15 07:47 pm (UTC)
Ooh, that's interesting. Thanks!
(Reply) (Parent) (Thread)
From: brittongregory
2006-06-15 08:08 pm (UTC)
Ugh. That's tricky. Of course, .NET (the tongue of choice in my group) doesn't have anything like generics or templates -- you have to either create a child to do the type-checking or do the (gulp!) Unsafe Cast of Evil. Still, I find templates so tremendously hideously ugly that I don't mind so much. Then again, I'm weird that way.
(Reply) (Parent) (Thread)