What's in my head?

The random ideas, questions, and thoughts that enter this feeble brain.

Saturday, September 24, 2005

Learn Java?

So I'm going to be on the recruiting loop and I figured I should get better at Design Patterns, Java, etc etc.. So I bought Head First Design Patterns, mostly because it was highly praised by D, a coworker and friend who knows his stuff. The book has so far been a really good read, even if the tone is a little first-year-ish.

It wasn't until I couldn't define finally() at work that I realized that perhaps I should brush up on my Java, as well. Maybe become a java certified developer...or something. What really interested me was when R showed me Java(TM) Puzzlers : Traps, Pitfalls, and Corner Cases yesterday. I was hooked on the very first "puzzle", which is more of a warm up than anything else. The problem was something like "Write a Java program that shows the change when you pay $2.00 for something that costs $1.10". Forget the lame symbols and decimal places for a sec, the point of the story was that

System.out.println( 2.00 - 1.10 );

doesn't work! It prints 0.899999999!! Imagine if you didn't remember THAT little tidbit when you are conducting interviews for a GIANT ONLINE RETAILER!! Moral of the story: 10^(-something) doesn't work too well in double. Either calculate in cents using int ( or long, or whatever ), or use BigDecimal. The real punchline for me was the gotcha for using BigDouble: use the BigDecimal( String ) constructor instead of the BigDecimal( double ) constructor, or you just end up wrapping your crappy problem with a big fancy class... yay computers.

BigDecimal twoDolla = new BigDecimal("2.00");
twoDolla = twoDolla.subtract(new BigDecimal("1.10"));
System.out.println( twoDolla );


Since I am ranting about Java, I must say that I love the new features of Java 5. Enums? Goooood! They're extendable, iterable, and more! Generics? Gooood! No more casting PLUS compile time type checking! Add those with the new enhanced for loop? JOY! But you see, there's just one problem...a problem that makes me very angry....
The new for loop works when the second argument in the parameter list is an instance of a class that implements the Iterable interface. I can see how this is a nice, clean definition, but please notice with me that an Iterator is not iterable. What does that mean? THE NEW FOR LOOP DOES NOT WORK WITH ITERATORS!!! ARRRRRRRRRRRG! Before I get hate mail, let me state that yes, you can't "iterate over an Iterator". I'm looking at it from a slightly higher level. A for loop should know what to do with an iterator, really.


Continue reading Learn Java? ...