pmn
pmn

Reputation: 1710

What parts of the Java ecosystem and language should a developer learn to get the most out of Scala?

Many of the available resources for learning Scala assume some background in Java. This can prove challenging for someone who is trying to learn Scala with no Java background.

What are some Java-isms a new Scala developer should know about as they learn the language?

For example, it's useful to know what a CLASSPATH is, what the java command line options are, etc...

Upvotes: 20

Views: 1269

Answers (3)

Madoc
Madoc

Reputation: 5919

That's a really great question! I've never thought about people learning Java just so they have it easier to learn Scala...

Apart from all the basics like for loops and such, learning Java Generics can be really helpful. The Scala equivalent is much more potent (and much harder to understand) than Java Generics. You might want to try to figure out where the limits of Java Generics are, and then in which cases Scala's type constructors can be used to overcome those limitations. At the more basic level, it is important to know why Generics are necessary, and how Java is a strongly typed language.

Java allows you to have multiple constructors for one class. This knowledge will be of no use when you learn Scala, because Scala has another way that allows you to offer several methods to create instances of a class. So, you'd rather not have a deep look into this Java concept.

Here are some concepts that differ very strongly between Java and Scala. So, if you learn the Java concepts and then later on want to learn the equivalent in Scala, you should be aware that the Scala equivalent differs so greatly from the Java version that a typical Java developer will have some difficulty to adapt to the Scala way of thinking. Still, it usually helps to first get used to the Java way, because it is usually simpler and easier to learn. I personally prefer to think of Java as the introductory course, and Scala is the pro version.

  • Java mutable collection concept vs. Scala mutable/immutable differentiation
  • static methods (Java) vs. singleton objects (Scala)
  • for loops
  • Java return statement vs. Scala functional style ("every expression returns a value")
  • Java's use of null for "no value" vs. Scala's more explicit Option type
  • imports
  • Java's switch vs. Scala's match

And here is a list of stuff that you will probably use from the Java standard library, even if you develop in Scala:

  • IO
  • GUI (Scala has a wrapper for Swing, but hey)
  • URLs, URIs, files
  • date
  • timers

And finally, some of Scala's features that have no direct equivalent in Java or the Java standard library:

  • operator overloading
  • implicits and implicit conversions
  • multiple argument lists / currying
  • anonymous functions / functions as values
  • actors
  • streams
  • Scala pattern matching (which rocks)
  • traits
  • type inference
  • for comprehensions
  • awesome collection operations like fold or map

Of course, all the lists are incomplete. That's just my view on what is important. I hope it helps.

And, by the way: You should definitely know about the class path and other JVM basics.

Upvotes: 9

VonC
VonC

Reputation: 1324713

The serie "Scala for Java Refugees" can gives some indications on typical Java topics you are supposed to know and how they translate into Scala.
For instance, the very basic main() Java function which translate into the Application trait, once considered harmful, and now improved (for Scala 2.9 anyway).

Upvotes: 4

Rafe Kettler
Rafe Kettler

Reputation: 76955

The standard library, above all else, because that's what Scala has most in common with Java.

You should also get a basic idea of Java's syntax, because a lot of books end up comparing something in Scala to something in Java. But other than the platform and some of the library, they're totally distinctive languages.

There are a few trivial conventions passed from one to the other (like command line options), but as you read books and tutorials on Scala you should pick those up as you go regardless of previous Java experience.

Upvotes: 4

Related Questions