I am reading about Scala here and there but I could not understand what would a Java developer gain from jumping on Scala.
I think it is something to do with functional programming.
Could someone please give me a concrete example of something I can not do in Java and going to Scala would save me?
This is not intented to be a critique on Java or something similar.I only need to understand the usage of Scala

- 18,826
- 34
- 135
- 254
-
2If you're interested in Scala, why don't you just follow a tutorial to learn what the language looks like and what features it has, and to see if you like it or not? Start at [www.scala-lang.org](http://www.scala-lang.org/) – Jesper Mar 06 '13 at 08:04
-
2I would follow the advice from the comments and start some research on the *huge* number of blogs dealing with your question. Then you can refer to stackoverflow for more specific questions whenever you need clarification. – pagoda_5b Mar 06 '13 at 08:12
-
also look at [this](http://stackoverflow.com/questions/727078/whats-so-great-about-scala) – pagoda_5b Mar 06 '13 at 08:13
-
Also [this](http://stackoverflow.com/questions/3844745/scala-advantages-after-java-having-closures/) – Tom Crockett Mar 06 '13 at 08:17
4 Answers
I'm also jumping from Java's world, the first thing that I think will save you is that Scala has a lot of compiler magic that helps you to keep your code simple and clean.
For example, the following is how Scala's case class will help you, we could define a class using this:
case class Student(name: String, height: Double, weight: Double)
instead of this:
class Student {
public final String name;
public final String height;
public final String weight;
public Student(String name, double height, double weight) {
this.name = name;
this.height = height;
this.weight = weight;
}
}
Yes, that is all, you don't need write constructor yourself, and you have all those equals
, hasCode
, toString
method for free.
It may looks like not a big deal in this simple case, but Scala really make you to model things a lot easier and quicker in OO, even if you are not using functional programming construct.
Also, high-order function and other functional programming construct will also give you powerful tools to solve your problem in Scala.
Update
OK, the following is a example of how functional programming will make your code more easier to understand and clean.
Functional programming is a large topic, but I found that even I'm from Java's world and does not understand what is monad or typeclass or whatever, Scala's functional programming construct is still help me to solve problem more easily and more expressive.
There are many times we need to iterate over a collection and do something to the elements in the collection, depends on a condition to decide what to do.
For a simple example, if we want to iterate over a List in java, and delete all file that size are zero. What we will do in Java maybe looks like the following:
List<File> files = getFiles()
for (File file: files) {
if (file.size() == 0) {
file.delete();
}
}
It's very easy and concise, isn't it? But with functional programming construct in Scala, we could do the following:
val files = getFiles()
val emptyFiles = files.filter(_.size == 0)
emptyFiles.foreach(_.delete())
As you can see, it has less code than the Java's version, and out intention is even clear -- we want filter out all files that size is 0, and for all of it, call File.delete()
on it.
It may looks weird at first, but once you get used to it and use it in right way, it will make your code a lot easier to read.
The technique is possible in Java (Function Java), but in the end it will looks like the following code:
list.filter(new Predicate<File>() {
public boolean predicate(File f) {
return f.size() == 0;
}
})
Which I'll just stick to the origin for-loop version, IMHO.

- 8,781
- 3
- 47
- 59
-
+1.Would it be possible to provide an example about functional programming – Jim Mar 06 '13 at 08:19
To get you started, this article by Graham Lea lists many ways that Scala can boost your productivity:
A New Java Library for Amazing Productivity.
It begins with:
- a broad and powerful collections framework
- collection methods that greatly reduce boilerplate
- immutable collections that don’t have mutation methods (unlike java.util classes where e.g. List.add() throws an exception if the list is immutable)
- an awesome switch-like function that doesn’t just match numbers, enums, chars and strings, but can succinctly match all kinds of patterns in lots of different classes, even in your own classes
- an annotation that automatically writes meaningful equals, hashCode and toString methods for classes whose fields don’t change (without using reflection)
- ...
and the list goes on.

- 21,002
- 4
- 67
- 80
To be specific on your question:
- in scala you can pattern match
- scala has higher order functions (which is not the same as java8 lambdas)
- anonymous function literals
- strong type inference
- lazy evaluation
- variance annotation
- higher kinded types
- mixin behavior with traits (stackable composition)
- implicit definition and conversion
- xml literals
- REPL
I could go on but I urge you to read at least some documentation.
For instance you can start here
-
But I didn't get what I would gain with functional programming that I don't with Java – Jim Mar 06 '13 at 08:13
-
@Jim It emphasizes on Immutability which greatly reduces bugs and reduces complexities of concurrency. I have found situations where applying the principles of functional programming in java has made my code easier too understand. – korefn Mar 06 '13 at 08:17