Alternative Programming Languages for JVM

Nandita Pandey
Analytics Vidhya
Published in
5 min readApr 13, 2020

What is Java Virtual Machine?

A Java virtual machine is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode.

Now the question arises how can Java Virtual Machine run programs written in other languages? Let us have a look into this by the help of a diagram.

Java Virtual Machine Concept

Alternative Programming Languages for JVM

The source code written in java gets compiled into application bytecode with the help of java compiler. The bytecode then runs in the java runtime that consists of the Standard libraries and Java Virtual Machine.

Now from the diagram we can see that the right hand side and the left hand side of the diagram are closely linked together. But the fact is that the right hand side of the diagram, the runtime stack, is capable of running the bytecode no matter from where this bytecode comes from. Mostly the bytecode is compiled from the java source code, but people started creating alternatives of java languages that can compile to java bytecode and hence can run on the same virtual machine.

The most commonly alternative programming languages used to obtain the java bytecode:

  1. Scala
  2. Groovy
  3. Kotlin

Even though these are different programming languages having different compilers, but compiling the source code written in these languages results in the same bytecode that can run on the Java Virtual Machine. These languages can make use of the Java Standard Libraries and can also take advantage of the Java Virtual Machine like speed and safety.

Reasons behind choosing alternative JVM languages

What are the reasons for choosing programming languages other than java for JVM, when java is the main language of the platform. Some of the reasons behind this:

  1. Productivity: Java is a programming language that has evolved since two decades and also we know that java is optimized for readability. Some people believe in programming languages that are more to the point, so that there teams can be more productive using those languages.
  2. Familiarity: Not every person knows how to program in java language. Also there is a variety of existing languages like Python and Ruby that too have same implementation as Java. In this case people who know these languages can get knowledge of the JVM versions of those languages like JPython and JRuby without learning a new programming language.
  3. Different Programming Paradigm: Not only object oriented programming languages are gaining fame but also functional programming are becoming popular. There are several other programming languages that are adopting new programming paradigm as well.

These are some reasons why people choose alternate languages while staying in Java Virtual Machine. When people do not want to code in java language, they can choose different language rather than changing the platform. Also because of these alternative JVM languages and their ideas, java has adopted a lot of changes as well.

Some Alternative JVM Languages

Let us have a brief look on the three major JVM alternative languages:

  1. Groovy

Groovy is a widely used JVM alternative language. It is a dynamic scripting language for JVM, which means the code written in groovy is interpreted at runtime rather than compiling it beforehand. Groovy is similar to Python and Ruby programming languages. Nevertheless, you can also compile the groovy code in bytecode using a groovy compiler. As interpreting the groovy code at runtime is quite slow, hence compiling the code usually results in better performance.

As we know Java is statically typed language, that means, we have to provide types for all the parameters which we are using in our code. However, in groovy we don’t have to provide all this, which makes the language more dynamic. Later groovy got an opt-in type system, in which the developer can optionally add a type annotations to the code and then these annotations can be used by the groovy compiler to find out the type related problems at the compile time in the code.

The groovy language is more to the point than java but the syntax is very close to that of java.

2. Scala

Scala is a programming language which combines object oriented programming language with procedural programming language, which Scala a very different language from java. Scala offers both functional programming features and advanced object oriented programming language features.

Scala is complied languages, the Scala compiler compiles the Scala source code into Java bytecode. Scala is statically typed language same as Java. Although Scala has a more extensive type system than Java so as to support both the functional programming features and advanced object oriented features.

The Akka (is a free and open-source toolkit and runtime simplifying the construction of concurrent and distributed applications on the JVM) library for concurrency and the Apache Spark library for big data processing are both written in Scala.

With its advanced object oriented features and functional programming features Scala can be great to work with. but, on the other hand, Scala is considered to be complex as well, as the compiler can be quite slow on some larger code bases because of the features the language is having.

3. Kotlin

Kotlin came into existence after Scala and also a recent addition to the set of JVM languages. Kotlin is developed by Jetbrains (same company that creates IntelliJ, the java IDE) and is supported by Google.

Kotlin does not relies on backward compatibility (refers to a hardware or software system that can successfully use interfaces and data from earlier versions of the system or with other systems) as java does, so the developers can choose the best ideas from the JVM only. Kotlin is interoperable with the Java, that means, whenever two java and kotlin files exist together they can share data with each other.

Kotlin source code can not only be compiled as java bytecode but also to JavaScript code which can be run on the browser.

These three are the majorly used alternative JVM languages, there are dozen of languages as well that can be used as alternatives.

--

--

Nandita Pandey
Analytics Vidhya

I’m an enthusiast learner. Here to share my learning to the people interested