background preloader

Scala for the Java Refugee

Facebook Twitter

Pt 1: main(String[]) 7 Jan 2008 This article is also available in Spanish and Bulgarian.

Pt 1: main(String[])

You know who you are. You’re the developer who picked up Java years ago, maybe as a second language and better alternative to C++, maybe as your first language coming into the industry. You’re comfortable with Java, you know its ins and outs, its moods. It’s like an old girlfriend; you may not feel the vibe anymore, but you know just how to rub it so it smiles. You’re starting to to become a bit pragmatic about your language choice though. The good news is that there’s light at the end of the tunnel. The only problem you have now is figuring out where to start. Have no fear, ye refugee of Java EE grid iron, all is not lost. Introductions If you’re like me and can identify with the above, then this series is for you.

Did I mention Alex’s Scala introduction series? Getting Started Nothing like getting things rolling with a little code. Notice the name of the file in question? Editors More Hello World Iterating Over an Array. Pt2. Basic OOP. 14 Jan 2008 In the previous installment, we looked at the basics of Scala syntax and provided some simple conceptual explanations.

Pt2. Basic OOP

Obviously there’s a lot more to this language than what I was able to present in a single (albeit very long) article. In this post we’re going to examine Scala’s object oriented constructs (classes, objects, methods, etc) and how they compare to Java. A More Complex Example Remember that Scala does not require every public class to be declared in a file of the same name. The first thing you should notice about this snippet is the package declaration. With that said, it’s still best-practice to separate your packages off into their own directories. To compile this class, we’re going to use the fsc command (short for Fast Scala Compiler). This command will compile all of the .scala files within the src/com/codecommit/examples/ directory and place the resultant .class files into bin/.

First Impressions. Pt. 3. Methods and Statics. 21 Jan 2008 In this series, we’ve already laid the foundations for Scala’s syntax as well as gotten a feel for how some of its object-oriented constructs work.

Pt. 3. Methods and Statics

We haven’t really looked at any of these subjects in-depth however. Most of our effort has been focused on high-level, flash bang overviews that just get you tasting your way into the language. This post will go into greater depth regarding method syntax, touch a bit on scopes and attempt to cover how static members work in Scala. We will also touch on a few gotchas due to “missing” imperative instructions. Methods Galore. Pt.4 - Pattern Matching and Exceptions. 28 Jan 2008 So far, we’ve been examining the similarities between Scala and Java.

Pt.4 - Pattern Matching and Exceptions

Areas where Scala syntax is so similar to Java’s as to be almost identical. Of course, this doesn’t take full advantage of the language, but it allows developers to utilize the cleaner Scala syntax within the restrictions of a familiar environment. From this point on, things are going to get a bit weird. A number of the “more advanced” features in the Scala language are clear departures from Java. Pattern Matching As is oft parroted on the blogosphere, pattern matching in Scala is really a lot like Java’s switch/case construct. It’s obviously an extremely naive prime number sieve (if you can call it that), but it demonstrates the example quite effectively. One of the major limitations of switch/case in Java (and really any C derivative language) is that it can only be used on primitives. Pt.5 - Traits and Types. 4 Feb 2008 One of the mantras repeated throughout this series has been that Scala is Java, just better.

Pt.5 - Traits and Types

Thus it stands to reason that Scala would at a minimum support all of the features and power that Java offers. So far you’ve seen things like object-oriented constructs, the power of methods and even dabbled a bit with pattern matching. Pt.6 - .Getting Over Java. 11 Feb 2008 Thus follows the sixth and final installment of my prolific “Scala for Java Refugees” series.

Pt.6 - .Getting Over Java

After this post, I will continue to write about Scala as the spirit moves, but I don’t think I’ll do another full-length series focused entirely on this one topic. It’s a surprisingly exhausting thing to do, tying yourself to a single subject for so long. (insert piteous moans from my keyboard) Anyway, enough of my whining… To be honest, I’ve been looking forward to this article from day one of the series. Class Extensions There has been some chit-chat around the Java communal fireplace talking about adding class extensions to Java 7. This funky looking sample is actually adding a new method to the String class (the one already defined by the language) and making it available to all instances of String in the defining scope.

Interop Between Java and Scala. 9 Feb 2009 Sometimes, the simplest things are the most difficult to explain.

Interop Between Java and Scala

Scala’s interoperability with Java is completely unparalleled, even including languages like Groovy which tout their tight integration with the JVM’s venerable standard-bearer. However, despite this fact, there is almost no documentation (aside from chapter 29 in Programming in Scala) which shows how this Scala/Java integration works and where it can be used. So while it may not be the most exciting or theoretically interesting topic, I have taken it upon myself to fill the gap.

Classes are Classes The first piece of knowledge you need about Scala is that Scala classes are real JVM classes. …and the second in Scala: Despite the very different syntax, both of these snippets will produce almost identical bytecode when compiled. …and from Java: In the case of either language, we can easily swap implementations of the Person class without making any changes to the call-site.

Traits are Interfaces Operators are Methods.