In this article Scala vs Java, we would be looking at two very popular programming languages, Scala and Java. What they are, where they are used, and finally, the advantages and disadvantages of using them.

Brief History

Scala was designed by Martin Odersky in 2001 at the  École Polytechnique Fédérale de Lausanne (EPFL) in Switzerland. It is based on addressing the shortcomings of java at the time, basic functional programming and the language Funnel. It was released internally in 2003, and later publicly released in 2004. The second version was out two years later.

Java, on the other hand, was written by James Gosling along with Patrick Naughton and Mike Sheridan, at Sun Microsystems in June 1991. Java was originally built for interactive television, but it was way too advanced for the digital cable television industry as at that time. The language was initially called Oak Programming Language after an oak tree that was outside Gosling’s office. Later the project went by the name Green and was finally renamed Java, from Java coffee. It was built to let developers “Write Once, Run Anywhere” (WORA).

Scala

Scala literally means Scalable Language. This shows that Scala Programming language has the capacity to grow with the growth in the technical, structural and functional parts of your program. The programmers can either play with the language by typing expressions of one line and be outputting the results or they use this language for full-scale technical programming exercises. Scala is used in the big data world, in the backend of applications, micro-services, build infrastructure, write service code and all structural and functional programming.

Pro’s

  • Support for functional programming.
  • Less boilerplate code.
  • Methods are public by default.
  • Faster in speed.
  • Verification of exceptions are not enforced.
  • Very extensible.
  • List Operators can be changed fluently.

Con’s

  • Scala is considered to be very complex.
  • Sometimes code can be unreadable.
  • ScalaDocs has been said to be somewhat incomplete.
  • Tools are few and not as abundant as Java.
  • The compilation is a bit slower than Java.
  • Binaries are not all backward compatible.

Java

Experts define Java as a General Purpose, inheritance supporting, object-oriented, Platform independent,  Architecturally neutral language. It is a dynamic, distributed and robust interpreted Programming Language; easily movable and able to run more than one task simultaneously sharing the same memory.  It is implemented as Stand-alone Applications, Desktop and Web Apps, Enterprise Application and Mobile Apps and Embedded Systems.

Pro’s

  • Very readable language.
  • Very structural.
  • Binaries are backward compatible.
  • Extensive and Abundance of Tools.
  • Faster compilation and run-time performance.
  • Platform independence.

Con’s

  • Not as productive as Scala.
  • Not so extensible as Scala.
  • Little or no solid support for functional programming.
  • Not built to deliver adequate support or asynchronous behavior.

Which is faster?

An open source developer, James Roper tried to find out whether Java was faster than Scala.  He ran a quick sort algorithm on the two languages. Here are the results:

Java Code

public static void quickSort(int[] array, int left, int right) {
    if (right <= left) {
        return;
    }
    int pivot = array[right];
    int p = left;
    int i = left;
    while (i < right) {
        if (array[i] < pivot) {
            if (p != i) {
                int tmp = array[p];
                array[p] = array[i];
                array[i] = tmp;
            }
            p += 1;
        }
        i += 1;
    }
    array[right] = array[p];
    array[p] = pivot;
    quickSort(array, left, p - 1);
    quickSort(array, p + 1, right);
}

Scala Code

def sortArray(array: Array[Int], left: Int, right: Int) {
  if (right <= left) {
    return
  }
  val pivot = array(right)
  var p = left
  var i = left
  while (i < right) {
    if (array(i) < pivot) {
      if (p != i) {
        val tmp = array(p)
        array(p) = array(i)
        array(i) = tmp
      }
      p += 1
    }
    i += 1
  }
  array(right) = array(p)
  array(p) = pivot
  sortArray(array, left, p - 1)
  sortArray(array, p + 1, right)
}

 

After the two executed, here is the resulting output in a chart.

scala vs java

this shows that Scala is 20% faster than Java and he describes what and what accounts for this difference in speed in his article.

Conclusion

Java is a general-purpose language and has really massive tools and support online; ideal for beginners in programming. Scala is highly recommended for non-beginners and people trying to get into functional programming. Both languages are very popular and very interesting to work with. If you have any thoughts or questions, you can leave a comment below.