

This expression is made with method calls since it is the same as the following expression, so + and * are valid identifiers in Scala. Take the following arithmetic expression: 1 + 2 * 3 / x In Scala, numbers are also treated as objects, so they also have methods. Unlike Java, which distinguishes primitive and reference types, Scala treats everything as an object, since Scala is a pure OOP language. Once we compile our program, we can run it with the scala command, which is just like the java command. This will generate some class files including one called HelloWorld.class. So, say we saved our Hello World program in a file called HelloWorld.scala. For Scala, the object files that its compiler produces are Java class files. For our Scala example above, we’d use scalac, which works like most compilers.
#Scala tutorial code#
Scala does not support static members, so we define them as singleton objects.Ĭompiling our code is also a bit different here. You may have also noted that the main method is not declared static. Here, we are defining a class called HelloWorld and an instance of that class of the same name. This introduces a singleton object, which is a class with a single instance.

The thing that may stand out to you is the use of the object declaration. Since the main method doesn’t return any value, its type is Unit. The method body has a single call to the method println with our greeting as the argument. So, what is going on here? Just like Java, the main method is the entry point for our Scala program, which takes an array of strings as its parameter. However, this is changing as more Java developers add Scala to their toolbelt.įor a practical example of how Java and Scala differ, let’s see how we’d create a list of Strings in both languages: One of its drawbacks, however, is community support: there is less detailed documentation, 3rd party libraries, and community presence than Java. All classes from the java.lang package are imported by default with Scala. One of the main advantages of Scala is that it can be executed on the Java Virtual Machine (JVM), making it very easy to interact with Java code. Scala uses Traits instead of Java interfaces.Any Scala method or function is treated like a variable, whereas Java treats them as objects.


Scala was designed to create a “better Java”, much like other alternatives like Kotlin and Ceylon. It requires a lot of lines of code to perform simple tasks. Java is known for its complexity and verbosity. This Scala course will help you stay ahead of the curve, make awesome, scalable apps, and learn a highly coveted programming language. Today, we will introduce you to Scala to make the transition. If you are a Java developer who wants to transition to Scala (or just see what it has to offer), you’re in the right place. In fact, according to StackOverflow’s 2020 survey, US Scala developers have the highest paying salaries. Scala is now recognized by big companies as a powerful language, namely Twitter and LinkedIn. Scala is a great option for Java developers who want to take their career to the next level (or for those who are just tired of Java’s quirks). Scala was designed to address some of the limitations and tediousness of Java. Scala is a general-purpose programming, type-safe JVM language language that offers support for object-oriented programming (OOP) and functional programming.
