Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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...

like image 682
pmn Avatar asked Jan 19 '11 22:01

pmn


People also ask

Why is Scala preferred over Java?

The Advantages of Scala Scala has an exact syntax, eliminating boilerplate code. Programs written in Scala require less code than similar programs written in Java. It is both an object-oriented language and a functional language. This combination makes Scala the right choice for web development.

Should I learn Java before Scala?

Hello, You can learn Scala without knowing Java. examples. Java standard library.

What is Scala best used for?

uses of Scala provides the best of both static and dynamic languages. It feels dynamic but is strongly statically typed language. Scala provides type inference for variables and functions, much better than limited type inference in Java and C#. It also provides a compiler that uses type reference to a full extent.


3 Answers

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.

like image 52
Madoc Avatar answered Nov 01 '22 18:11

Madoc


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.

like image 41
Rafe Kettler Avatar answered Nov 01 '22 18:11

Rafe Kettler


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).

like image 40
VonC Avatar answered Nov 01 '22 20:11

VonC