This article introduces Scala to those Java developers who don't know Scala. I assume here that you already know Java (preferably Java 8) so that you can compare the features of Scala with Java. Please be informed that this article is not an end to end Scala tutorial. I am covering only the fundamentals of Scala which are used in my Apache Spark tutorials.
First of all, remember that Scala is a JVM based language which is running on top of your regular Java Virtual Machine. The whole purpose of Scala is providing a convenient functional programming language (at that time Java 8 wasn't there). However since Scala is built on top of Java, you can access Java libraries and API from your Scala code.
To play with Scala, please setup Scala on IntelliJ IDEA or install a command line Scala version in your system.
Main Function
Let's start with a HelloWorld program in Scala:
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello World!")
}
}
The same program in Java looks like this:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
The first thing a Java developer notices in a Scala program is the keyword object in place of class. This is because Scala is more object-oriented than Java and there is no way to define a static member in Scala. Therefore, you need to have an object with the non-static main function as defined in the above example to run a Scala program. However, nothing prevents you from defining your own class.
The data type in a variable definition is optional. You can rewrite the above code as given below without specifying the variable type.
I stop here as of now. However, whenever I use a new Scala feature in my series of Apache Spark article, I will describe them here. The official Scala Documentation is another good resource to learn Scala. As I have mentioned earlier, this article is not a complete Scala cookbook. It is only a cheat sheet for my Apache Spark articles.
object HelloWorld {
def main(args: Array[String]): Unit = {
val person: Person = new Person();
person.sayHello()
}
}
class Person {
def sayHello(): Unit = {
println("Hello")
}
}
Data Types
Scala supports all the data types available in Java. Remember that everything is Object in Scala. Therefore there is no primitive equivalent in Scala; instead Scala provides basic data types equivalent to Java wrapper classes.Scala | Java | Example |
---|---|---|
Byte | java.lang.Byte | val data: Byte = 127 |
Short | java.lang.Short | val data: Short = 128 |
Int | java.lang.Integer | val data: Int = 256 |
Long | java.lang.Long | val data: Long = 1024L |
Float | java.lang.Float | val data: Float = 3.25F |
Double | java.lang.Double | val data: Double = 7.21 |
Char | java.lang.Character | val data: Char = 'A' |
Boolean | java.lang.Boolean | val data: Boolean = true |
String | java.lang.String | val data: String = "Bob" |
Variable Declaration
Java uses thefinal
keyword to define constant variables. In Scala, the val
is used to define constant values as shown below.object HelloWorld {
def main(args: Array[String]): Unit = {
val name: String = "Bob"
/* name = "Fred" // Cannot reassign val */
println(name)
}
}
If you want to reassign a variable later in the code, use var
to define the variable.object HelloWorld {
def main(args: Array[String]): Unit = {
var name: String = "Bob"
name = "Fred"
println(name)
}
}
object HelloWorld {
def main(args: Array[String]): Unit = {
var name = "Bob"
name = "Fred"
println(name)
}
}
For Loop
Similar to Java, Scala supports For loop, While loop and Do-While loop. Among them, only the syntax of For loop differs from the syntax of Java. Compare the following Java code to print numbers 0 to 9 with the Scala code given underneath.class HelloWorld {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}
object HelloWorld {
def main(args: Array[String]): Unit = {
for (i <- 0 until 10) {
println(i)
}
}
}
If you want to include the upper bound ( in other words to print 0 to 10), replace the keyword until
by to
as given below:object HelloWorld {
def main(args: Array[String]): Unit = {
for (i <- 0 to 10) {
println(i)
}
}
}
Lambda Expression & Method Reference
Lambda expression and method reference are key requirements for functional programming. Suppose you have a list of numbers, you can print the even values using stream operations in Java as shown below:import java.util.List;
class HelloWorld {
public static void main(String[] args) {
List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
list.stream()
.filter(x -> x % 2 == 0) // Lambda expression
.forEach(System.out::println); // Method reference
}
}
The same code can be written in Scala as given below:object HelloWorld {
def main(args: Array[String]): Unit = {
val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
list.filter(x => x % 2 == 0) // Lambda expression
.foreach(println) // Function reference
}
}
The arrow operator in the lambda expression is different from Java. The functional programming concept is quite similar in both languages but Scala code looks more native to functional programming.Tuples
The Tuple is a Scala-specific data type to represent a fixed number of elements. Like most other data types in Scala, Tuples are immutable. The following example defines a Tuple with two elements:String
and Int
.object HelloWorld {
def main(args: Array[String]): Unit = {
val person = ("Alice", 12)
println(person._1) // Alice
println(person._2) // 12
}
}
Array
The syntax of defining and accessing an Array in Scala is different from Java. The following Java code creates an integer array, prints the first element and then print all the elements using a for-each loop.class HelloWorld {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
System.out.println("First element " + array[0]);
for (int i : array) {
System.out.println(i);
}
}
}
The same logic is written in the following Scala code. Notice the parenthesis in place of square brackets when accessing an array element in Scala. The syntax of the for-each loop also differs from Java.object HelloWorld {
def main(args: Array[String]): Unit = {
val array = Array(1, 2, 3, 4, 5)
println("First element " + array(0))
for (i <- array) {
println(i)
}
}
}
Formatted Output
Filling a String template with variables is frequently required for formatted output. In Java, you can do this using either theString.format
method or using System.out.format
method.class HelloWorld {
public static void main(String[] args) {
String name = "Alice";
int age = 12;
String output = String.format("%s is %d years old", name, age);
System.out.println(output);
System.out.format("%s is %d years old\n", name, age);
}
}
The String Interpolation feature in Scala makes life easier. Adding the letter s
in front of a String value lets you to interpolate variables directly into the String as shown below:object HelloWorld {
def main(args: Array[String]): Unit = {
val name = "Alice"
val age = 12
val output = s"$name is $age years old"
println(output)
println(s"$name is $age years old")
}
}
You can also do inline operations or call functions using a curly bracket in String Interpolation as shown below:object HelloWorld {
def main(args: Array[String]): Unit = {
val name = "Alice"
val age = 12
println(s"$name is ${age * 2} years old")
}
}
I stop here as of now. However, whenever I use a new Scala feature in my series of Apache Spark article, I will describe them here. The official Scala Documentation is another good resource to learn Scala. As I have mentioned earlier, this article is not a complete Scala cookbook. It is only a cheat sheet for my Apache Spark articles.
EmoticonEmoticon