Scala Vs Java

After reviewing and understanding what is Scala in our previous lecture where we knew what is Scala now let’s see the similarities, and differences that you will face when you try to write code in Scala. (As I mentioned before this maybe will have sense to you if you come from a Java background).

This article is based on the Scala-lang.org Scala for Java Developers. Check it out to know more about the new Scala Features.

High-level similarities with Java

At a high level, Scala shares these similarities with Java:

  • Scala code is compiled into .class files, packaged in JAR files, and runs on the JVM.
  • It’s an object-oriented programming (OOP) language
  • It’s statically typed.
  • Both languages have support for lambdas and higher-order functions (HOF).
  • They can both be used with trend IDEs like IntelliJ IDEA and Microsoft VS Code.
  • Projects can be built with build tools like Gradle, Ant, and Maven.
  • It has terrific libraries and frameworks for building server-side, network-intensive applications, including web server applications, microservices, machine learning, and more.
  • Both Java and Scala can use Scala libraries.
  • Scala can seamlessly use the wealth of libraries that have been developed for Java.

High-level differences with Java

Now let’s review the High-level differences between Java and Scala:

  • Scala has a concise but readable syntax; we call it expressive. This means that the code written in Scala is easy to read and understand by developers. The syntax of Scala is designed to be more concise than that of some other programming languages, which means that shorter and more efficient programs can be written in terms of lines of code. However, despite this conciseness, Scala code is still easy to read and understand, making programming in Scala more enjoyable and productive for developers.
  • Though it’s statically typed, Scala often feels like a dynamic language. Scala does have some dynamic features, which makes it more flexible and dynamic in certain situations. For example, Scala has the ability to use reflective programming, which allows the program to inspect and modify its own structure at runtime. Additionally, Scala has the ability to use closures and higher-order functions, which means that functions can be passed around as values and used dynamically in a program.
  • Scala is a pure OOP language, so every object is an instance of a class, and symbols like + and += that look like operators are really methods; this means that you can create your own operators. For example, if you create a class called MyNumber and define a method called + that adds two numbers, you can use the + operator on objects of the MyNumber class to add them together.
  • In addition to being a pure OOP language, Scala is also a pure FP language (Functional Programming); in fact, it encourages a fusion of OOP and FP, with functions for logic and objects for modularity.
  • Scala has a full suite of immutable collections, including ListVector, and immutable Map and Set implementations.
  • Everything in Scala is an expression: constructs like if statements, for loops, match expressions, and even try/catch expressions all have return values.
  • Scala idioms favor immutability by default: you’re encouraged to use immutable (final) variables and immutable collections
  • Idiomatic Scala code does not use null, and thus does not suffer from NullPointerException.
  • The Scala ecosystem has other build tools in sbt (Scala Build Tool), Mill (if you want to learn more about Mill you can check their website), and others.
  • In addition to running on the JVM, the Scala.js project lets you use Scala as a JavaScript replacement.
  • The Scala Native project adds low-level constructs to let you write “systems” level code, and also compiles to native executables.

Programming level differences

Finally, let’s review some of the differences you’ll see every day when writing code in Scala:

  • Scala’s syntax is extremely consistent
  • Variables and parameters are defined as val (immutable, like final in Java) or var (mutable).
  • Type inference makes your code feel dynamically typed, and helps to keep your code brief.
  • In addition to simple for loops, Scala has a powerful for comprehensions that yield results based on your algorithms.
  • Pattern matching and match expressions will change the way you write code.
  • Writing immutable code by default leads to writing expressions rather than statements; in time you see that writing expressions simplifies your code (and your tests).
  • Top-level definitions let you put method, field, and other definitions anywhere, also leading to concise, expressive code.
  • You can create mixins by “mixing” multiple traits into classes and objects (traits are similar to interfaces in Java 8 and newer).
  • Classes are closed by default, supporting Joshua Bloch’s Effective Java idiom, “Design and document for inheritance or else forbid it”.
  • Scala’s contextual abstractions and term inference provide a collection of features:
    • Extension methods let you add new functionality to closed classes.
    • Given instances let you define terms that the compiler can synthesize at using points, making your code less verbose and essentially letting the compiler write code for you
    • Multiversal equality lets you limit equality comparisons—at compile time—to only those comparisons that make sense.
  • Scala has state-of-the-art, third-party, open-source functional programming libraries.
  • Scala case classes are like records in Java 14; they help you model data when writing FP code, with built-in support for concepts like pattern matching and cloning.
  • Thanks to features like by-name parameters, infix notation, optional parentheses, extension methods, and higher-order functions, you can create your own “control structures” and DSLs.
  • Scala files do not have to be named according to the classes or traits they contain.
  • Many other goodies: companion classes and objects, macros, union and intersection, numeric literals, multiple parameter lists, default values for parameters, named arguments, and more.

So what do you thing about Scala now? it looks easy. In the next we compare some code in Scala and Java.

Leave a comment