Scala Pitfalls For Java Refugees (SP4JR) #0: because what you think you know might be a hammer waiting for a thumb
Java++… or something else?
This article is the first of what should be a really short series, because as you know, Scala is just Java++, and so the differences between the two are almost non-existent.
OK, I don’t think I could have said more wrong things in one sentence, so now, let’s be serious!
Scala is a great language. But at some point, it seems to have been advertised as (only) being a kind of Java++, what is utterly wrong and leads to bad expectations, which in turns leads to bad experiments and disappointment.
There is a swarm of reasons for that superstition, from early advertising by some Scala users, to the same bad advertising from Scala detractors, with the added confusion of a lot of ‘similarities’ between the two languages: both are statically typed, are compiled, run on the JVM, share some keywords, are Object Oriented, and so on.
So… where are the differences?
But when you look at the details, where the devil lies, you will see that there are a lot of differences. Just in the above ‘similarities’, each of them is more a likeness in wording than in the actual concept:
- they both run on the JVM – and its ‘J’ part is quite heavy. To start with, it is not that friendly with foreign languages, although it’s getting better. Moreover, at the end of the compilation, whatever the language may have been saying, it’s the bytecode which is speaking. And sometimes it may be quite surprising, as we will see about visibility modifiers;
- they are both Object Oriented, but Scala is both more pure than Java in that respect, and loaded with a lot of new concepts that have their subtleties. Sometimes, to fully understand why you get that NullPointerException, you will have to exactly understand these subtleties;
- they are both statically typed, but I believe that Java is one of the worst things that happened to static typing, and Scala one of the best… So you will have to unlearn your a priori and learn what is real about static typing again, the first thing being: the type system is your friend, use it to all extents, and it is not something that stands high between your dreamed code and the actual reality;
- they share some (a lot of) keywords… but when you only know Java you may be surprised by their semantics in Scala, and face unexpected results, just because you haven’t even considered that return is something completely different in each language.
Learning your new hammer
This series of articles will try to highlight some of these differences, so that you may have an idea in a glimpse of what may be unexpected. Of course, that may spoil some fun and WTF moments, so, please do not read the following articles if you want to leave the suspense untouched!
The goal is not to teach Scala: there are a lot of excellent tutorials around the net for that – one really good one among others being Scala For Java Refugees, from which I stole the title. So, a basic knowledge of Scala syntax will be assumed.
Hoping it will help, see you in the first article, SP4JR #1: “you don’t want to use return”.