A penny for thoughts?

About the correct valuation

It’s shit like this Java

2 comments

Sure this is a minor quirk and there are plenty of more annoying things to bitch about when it comes to Java but it’s the collection of all these stupid decisions that makes Java annoying to use.

Have you every heard of Arrays.copyOfRange? Apparently it’s new in Java 6. Apparently before Java 6 there was a completely different method of doing it: System.arraycopy

Anyway, I was using Arrays.copyOfRange today as it’s typically implemented in pretty much every programming language I know of. If you do something like split(0,0) it should give you the first element. But in Java, no it doesn’t do that. It gives you nothing at all. Because the to is inclusive but the from is exclusive.

So basically if you want the whole array, instead of 0 to length – 1 which you would expect to need to provide (I want from this index to that index), you have to ask for index 0 to index end + 1 which is about the least intuitive thing I can think of for an array split. I totally would’ve expected an OutOfBoundsException.

On the other hand, if we take a look at how System.arraycopy works, you basically have to specify your source array, pass in a destination array Java can write to, tell it where to start in each and the length. Saner, but still a pain to actually use and not the least bit typesafe.

Why can’t Java convenience methods be convenient? As far as I can tell, one the the few things they’ve gotten right recently is the for(type var : collection) notation which is actually quite nice to use when you’re iterating over a collection. (Though why they overloaded for instead of using something like foreach(type var in collection) I have no idea.)

Written by Smokinn

April 20th, 2011 at 5:02 am

Posted in Uncategorized

2 Responses to 'It’s shit like this Java'

Subscribe to comments with RSS or TrackBack to 'It’s shit like this Java'.

  1. http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

    Excluding the upper bound is the norm rather than the exception (think of Python’s range() function). This means that array[m:n] + array[n:p] does not duplicate nth element. This is cleaner in the opinion of many to explicitly excluding one of the extreme bounds by subtracting or adding 1.

    gnuvince

    20 Apr 11 at 8:19 am

  2. Maybe in a few years Java will have all the useful methods that dynamic languages have had for years.

    I hear that in Java 7 you can actually use Strings in switch statements !!1!!1!!!! #OMGWTFBBQ

    trulogik

    22 Apr 11 at 7:25 am

Leave a Reply