What other languages does the JVM support

Time:2021-3-6

In order to make Java language have good cross platform ability, Java provides a kind of intermediate code — bytecode, which can be used on all platforms.

With bytecode, no matter which platform (such as windows, Linux, etc.), as long as the virtual machine is installed, you can directly run bytecode.

Similarly, bytecode also decouples the Java virtual machine from the Java language. This may not be understood by many people. Isn’t Java virtual machine the one that runs Java language? What does this decoupling mean?

In fact, Java virtual machine can support many languages other than Java, such as kotlin, groovy, jruby, Jython, Scala, etc. They are supported because these languages can also be compiled into bytecodes. The virtual machine doesn’t care which language the bytecode is compiled from.

Developers who often use ide may find that when we want to create a Java class with the right mouse button in intelij idea, IDE will also prompt to create other types of files. This is the default language supported by ide that can run on the JVM. If there is no prompt, it can be supported by plug-ins.

At present, there are many languages that can run directly on the JVM. Today I will introduce nine of them. Each language is demonstrated by a piece of “Hello world” code to see how the syntax of different languages is different.

Kotlin

Kotlin is a static type programming language running on Java virtual machine, which can also be compiled into javascript source code. Kotlin was originally designed to produce programs with high performance requirements, so it runs on a par with Java. Kotlin can be used as a plug-in from JetBrains inteillij idea ide.

Hello World In Kotlin

fun main(args: Array<String>) {
    println("Hello, world!")
}

Groovy

Apache’s groovy is an object-oriented programming language designed on the Java platform. Its syntax style is very similar to Java. Java programmers can quickly use groovy skillfully. In fact, the groovy compiler can accept completely pure Java syntax format.

An important feature of using groovy is the use of type inference, which enables the compiler to infer the type of a variable when the programmer does not specify it. Groovy can use libraries written in other java languages. The syntax of groovy is very similar to Java, and most java code also matches the syntax rules of groovy, although the semantics may be different.

Hello World In Groovy

static void main(String[] args) {
    println('Hello, world!');
}

Scala

Scala is a multi paradigm programming language, which is designed to integrate various features of object-oriented programming and functional programming.

Scala is often described as a multimodal programming language because it mixes features from elements of many programming languages. But in any case, it is essentially a pure object-oriented language. Compared with traditional programming languages, it provides a good framework for parallel programming. Scala code can be well optimized into bytecode and run as fast as native Java.

Hello World In Scala

object HelloWorld {  
    def main(args: Array[String]) {  
       System.out.println("Hello, world!");  
    }  
 } 

Jruby

Jruby is used to bridge Java and ruby. It uses a shorter syntax than groovy to write code, enabling each line of code to perform more tasks. Just like ruby, jruby doesn’t just provide advanced syntax formats. It also provides pure object-oriented implementation, closure and so on. Compared with ruby itself, jruby has many Java based class libraries that can be called. Although Ruby also has many class libraries, it can’t compare with Java standard class libraries in quantity and universality.

Hello World In Jruby

"puts 'Hello, world!'"

Jython

Jython is a Python interpreter written in Java. Jython can use Python language to generate dynamic compiled Java bytecode efficiently.

Hello World In Jython

print "Hello, world!"

Fantom

Fantom is a general object-oriented programming language, which is created by Brian and Andy frank and runs on Java runtime environment, JavaScript and. Net common language runtime. Its main design goal is to provide standard library API to abstract whether the code will eventually run on JRE or CLR.

Fantom is the same object-oriented programming language as groovy and jruby, but the tragedy is that fantom can’t use Java class library, but uses its own extended class library.

Hello World In Fantom

class Hello
{
  static Void main() { echo("Hello, world!") }
}

Clojure

Clojure is a modern, functional and dynamic dialect of LISP programming language on Java platform. Like other lisps, clojure regards code as data and has a lisp macro system.

Although clojure can also be directly compiled into Java bytecode, it cannot use dynamic language features and call Java class libraries directly. Unlike other JVM scripting languages, clojure is not object-oriented.

Hello World In Clojure

(defn -main [& args]
    (println "Hello, World!"))

Rhino

Rhino is a JavaScript engine written entirely in Java and currently managed by the Mozilla foundation.

Rhino features a shell for JavaScript, and then embedded into Java, so that Java programmers can use it directly. Among them, rhino’s javaadapters enable JavaScript to achieve specific functions by calling Java classes.

Hello World In Rhino

 print('Hello, world!')

Ceylon

Ceylon is an object-oriented programming language with strong static type, which emphasizes invariance and is created by red hat. Ceylon program runs on Java virtual machine and can be compiled into JavaScript. Language design focuses on source code readability, predictability, scalability, modularity and metaprogramming.

Hello World In Ceylon

 shared void run() {
    print("Hello, world!");
}

summary

Well, these are the current mainstream 9 languages that can run on the JVM. Plus Java, there are just 10. If you are a java developer, it is necessary to master one of the above 9, so that you can have more choices in some scenarios with special needs. It is recommended to choose one of groovy, Scala and kotlin.

Original address:www.manongjc.com/article/106986.htm…

This work adoptsCC agreementReprint must indicate the author and the link of this article