An Overview of the JVM Languages – JVM语言概述

最后修改: 2018年 4月 14日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

Besides Java, other languages can run on the Java Virtual Machine like Scala, Kotlin, Groovy, Clojure.

除了Java,其他语言也可以在Java虚拟机上运行,如Scala、Kotlin、Groovy、Clojure。

In the following sections, we’ll take a high-level look at the most popular JVM languages.

在下面的章节中,我们将对最流行的JVM语言做一个高层次的考察。

Of course, we’ll start with the forerunner of JVM languages – Java.

当然,我们将从JVM语言的先驱者–Java开始。

2. Java

2.Java

2.1. Overview

2.1.概述

Java is a general-purpose programming language embracing the Object-Oriented Paradigm.

Java是一种拥抱面向对象范式的通用编程语言。

A core feature of the language is the cross-platform portability, which means that programs written on one platform are executable on any combination of software and hardware with adequate runtime support. This is achieved by compiling code into bytecode first, instead of directly to platform-specific machine code.

该语言的一个核心特征是跨平台的可移植性,这意味着在一个平台上编写的程序可以在任何有足够运行时间支持的软件和硬件组合上执行。这是通过将代码先编译成字节码,而不是直接编译成特定平台的机器码来实现的。

Java bytecode instructions are analogous to the machine code, but they’re interpreted by a Java Virtual Machine (JVM) specific to the host operating system and hardware combination.

Java字节码指令类似于机器代码,但它们是由特定于主机操作系统和硬件组合的Java虚拟机(JVM)解释的。

Although originally an object-oriented language, Java has started adopting concepts from other programming paradigms like functional programming.

虽然最初是一种面向对象的语言,但Java已经开始采用其他编程范式的概念,如函数式编程

Let’s have a quick look at some of Java’s main features:

让我们快速浏览一下Java的一些主要功能。

  • Object-oriented
  • Strongly statically typed
  • Platform-independent
  • Garbage-collected
  • Multithreaded

2.2. Examples

2.2.实例

Let’s see how a simple “Hello, World!” example looks like:

让我们看看一个简单的 “你好,世界!”的例子是什么样子。

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

In this example, we’ve created a class named HelloWorld and defined the main method which prints a message on the console.

在这个例子中,我们创建了一个名为HelloWorld的类,并定义了main方法,在控制台打印一条信息。

Next, we’ll use the javac command to generate the bytecode which can be executed on a JVM:

接下来,我们将使用javac命令来生成可在JVM上执行的字节码

javac HelloWorld.java

Finally, the java command executes the generated bytecode on JVM:

最后,java命令在JVM上执行生成的字节码:

java HelloWorld

For more Java examples, check out our list of tutorials.

关于更多的Java例子,请查看我们的教程列表

3. Scala

3.规模

3.1. Overview

3.1.概述

Scala stands for “scalable language”. Scala’s a statically typed language which combines two important programming paradigms, namely object-oriented and functional programming. 

Scala代表着 “可扩展的语言”。Scala是一种静态类型的语言,它结合了两种重要的编程范式,即面向对象和函数式编程。

The language originated in 2004 but became more popular in recent years.

该语言起源于2004年,但在最近几年变得更加流行。

Scala is a pure Object-Oriented language as it doesn’t support primitives. Scala provides the ability to define classes, objects, methods along with functional programming features like traits, algebraic data types, or type classes.

Scala是一种纯面向对象的语言,因为它不支持原语。Scala提供了定义类、对象、方法以及函数式编程特征的能力,如特质、代数数据类型或类型类。

A few important features of Scala are:

Scala的几个重要特点是。

  • Functional, Object-oriented
  • Strongly statically typed
  • Algebraic Data Types
  • Pattern Matching
  • Enhanced Immutability Support
  • Lazy Computation
  • Multithreaded

3.2. Examples

3.2.实例

First, let’s take a look at the same “Hello, World!” example as before, this time in Scala:

首先,让我们看看与之前一样的 “Hello, World!”的例子,这次是用Scala语言。

object HelloWorld {
    def main(args: Array[String]): Unit = println("Hello, world!")
}

In this example, we’ve created a singleton object named HelloWorld and the main method.

在这个例子中,我们创建了一个名为HelloWorld的单子对象和main方法。

Next, to compile this, we can use scalac:

接下来,为了编译这个,我们可以使用scalac

scalac HelloWorld.scala

The scala command executes the generated bytecode on JVM:

scala命令在JVM上执行生成的字节码。

scala HelloWorld

4. Kotlin

4、科特林

4.1. Overview

4.1.概述

Kotlin is a statically typed, general-purpose, open-source language developed by JetBrains team, which brings together the Object-oriented and functional paradigms.

Kotlin是一种静态类型的通用开源语言,由JetBrains团队开发,它汇集了面向对象和函数式范例。

The main focus while developing Kotlin was Java interoperability, safety (exception handling), conciseness and better tooling support.

开发Kotlin的主要重点是Java的互操作性、安全性(异常处理)、简洁性和更好的工具支持。

Since the release of Android Studio 3.0, Kotlin is a fully supported programming language by Google on the Android Platform. It’s also included in the Android Studio IDE package as an alternative to the standard Java compiler.

自从Android Studio 3.0发布以来,Kotlin是谷歌在Android平台上完全支持的一种编程语言。它也被包含在Android Studio IDE软件包中,作为标准Java编译器的替代品。

Some important Kotlin features:

一些重要的Kotlin特性。

  • Object-oriented + Functional
  • Strongly statically typed
  • Concise
  • Interoperable with Java

Our introduction to Kotlin also contains more specifics on the features.

我们的Kotlin介绍也包含了更多关于功能的具体内容。

4.2. Examples

4.2.实例

Let’s see the “Hello, World!” example in Kotlin:

让我们看看Kotlin中的 “Hello, World!”例子。

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

We can write the code above in a new file called helloWorld.kt.

我们可以在一个名为helloWorld.kt.的新文件中编写上述代码。

Then, we’ll use the kotlinc command to compile this and generates bytecode which can be executed on a JVM:

然后,我们将使用kotlinc命令来编译这个并生成可以在JVM上执行的字节码。

kotlinc helloWorld.kt -include-runtime -d helloWorld.jar

The -d option is used to indicate the output file for class files or a .jar file name. The -include-runtime option makes the resulting .jar file self-contained and runnable by including the Kotlin runtime library in it.

-d选项用于指示class文件的输出文件或.jar文件名。-include-runtime选项使生成的.jar文件自成一体并可运行,因为其中包含了Kotlin运行时库。

Then, the java command executes the generated bytecode on JVM:

然后,java命令在JVM上执行生成的字节码。

java -jar helloWorld.jar

Let’s also see another example of printing a list of items using a for loop:

我们也来看看另一个使用for循环打印项目列表的例子。

fun main(args: Array<String>) {
    val items = listOf(1, 2, 3, 4)
    for (i in items) println(i)
}

5. Groovy

5 格罗维

5.1. Overview

5.1.概述

Groovy is an object-oriented, optionally typed, dynamic domain specific language (DSL), with support for static-typing and static compilation capabilities. It’s aimed at improving developer productivity, with easy to learn syntax.

Groovy是一种面向对象、可选择类型的动态特定领域语言(DSL),支持静态类型化和静态编译功能。它的目标是提高开发人员的工作效率,语法简单易学。

Groovy integrates easily with any Java program and immediately adds powerful features, like scripting capabilities, runtime and compile-time meta-programming and functional programming features.

Groovy很容易与任何Java程序集成,并立即增加了强大的功能,如脚本功能、运行时和编译时元编程和功能编程功能。

Let’s highlight a few important features:

让我们强调几个重要的特点。

  • Object-oriented with functional features like Higher-order functions, currying, closures
  • Typing – Dynamic, Static, Strong, Duck
  • Domain Specific Language
  • Interoperability with Java
  • Productivity by conciseness
  • Operator overloading

5.2. Examples

5.2.实例

First, let’s see our “Hello, World!” example in Groovy:

首先,让我们看看Groovy中的 “Hello, World!”例子。

println("Hello world")

We wrote the above code in a new file called HelloWorld.groovy. Now we can run this code in two ways: by compiling then executing or by just running uncompiled code.

我们将上述代码写在一个名为HelloWorld.groovy的新文件中。现在我们可以通过两种方式来运行这段代码:先编译后执行或者直接运行未编译的代码。

We can compile a .groovy file using the groovyc command as follows:

我们可以使用groovyc命令来编译.groovy文件,如下所示。

groovyc HelloWorld.groovy

Then, we’ll use the java command for executing the groovy code:

然后,我们将使用java命令来执行groovy代码。

java -cp <GROOVY_HOME>\embeddable\groovy-all-<VERSION>.jar;. HelloWorld

For example, the command above could look like:

例如,上面的命令可能看起来像。

java -cp C:\utils\groovy-1.8.1\embeddable\groovy-all-1.8.1.jar;. HelloWorld

Let’s also see how we can use the groovy command to execute the .groovy file without compiling:

我们也来看看如何使用groovy命令来执行.groovy文件,而无需编译。

groovy HelloWorld.groovy

Finally, here’s another example of printing a list of items with the index:

最后,这里是另一个打印带有索引的项目列表的例子。

list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list.eachWithIndex { it, i -> println "$i: $it"}

Have a look at more Groovy examples in our introduction article.

在我们的介绍文章中看看更多的Groovy例子。

6. Clojure

6.Clojure[/strong

6.1. Overview

6.1.概述

Clojure is a general-purpose functional programming language. The language runs on JVM as well as Microsoft’s Common Language Runtime. Clojure is a compiled language still, it remains dynamic, as its features are supported at runtime.

Clojure是一种通用的函数式编程语言。该语言在JVM以及微软的通用语言运行时上运行。Clojure仍然是一种编译语言,它仍然是动态的,因为其功能在运行时得到支持。

The Clojure designers wanted to design modern Lisp which can run on JVM. That’s why it’s also known as a dialect of the Lisp programming language. Similar to Lisps, Clojure treats code as data and has a macro system as well.

Clojure的设计者希望设计出能够在JVM上运行的现代Lisp>。这就是为什么它也被称为Lisp编程语言的一种方言。与Lisp类似,Clojure将代码视为数据,并且也有一个宏系统。

Some important Clojure features:

一些重要的Clojure特性。

  • Functional
  • Typing – Dynamic, Strong, recently started supporting gradual typing
  • Designed for concurrency
  • Runtime polymorphism

6.2. Examples

6.2.实例

Unlike other JVM languages, it’s not that straightforward to create simple “Hello, World!” program in Clojure.

与其他JVM语言不同,在Clojure中创建简单的 “Hello, World!”程序并不是那么简单的事情。

We’ll use the Leiningen tool for running our example.

我们将使用Leiningen工具来运行我们的例子。

First, we’ll create a simple project with default template using the following command:

首先,我们用以下命令创建一个带有默认模板的简单项目。

lein new hello-world

The project will be created with following file structure:

该项目将以下列文件结构创建。

./project.clj
./src
./src/hello-world
./src/hello-world/core.clj

Now we need to update the ./project.ctj file with the following content to set the main source file:

现在我们需要用以下内容更新./project.ctj文件,以设置主源文件。

(defproject hello-world "0.1.0-SNAPSHOT"
  :main hello-world.core
  :dependencies [[org.clojure/clojure "1.5.1"]])

Now we’re set to update our code to print “Hello, World!” in the ./src/hello-world/core.clj file:

现在我们准备更新我们的代码,在./src/hello-world/core.clj文件中打印 “Hello, World!”。

(ns hello-world.core)

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

Finally, we’ll use the lein command to execute the code above, after moving to the project’s root directory:

最后,我们将使用lein命令来执行上面的代码,然后移动到项目的根目录。

cd hello-world
lein run

7. Other JVM Languages

7.其他JVM语言

7.1. Jython

7.1 Jython

Jython is the Java platform implementation of Python which runs on the JVM.

JythonPython的Java平台实现,在JVM上运行。

This language was initially designed to enabled writing high-performance application without sacrificing interactivity. Jython is object-oriented, multi-threaded and uses Java’s garbage collector for efficiently cleaning up the memory.

这种语言最初是为了在不牺牲互动性的前提下,使编写高性能的应用程序。Jython是面向对象的、多线程的,并使用Java的垃圾收集器来有效地清理内存。

Jython includes most of the modules which are part of the Python language. It can also import and use any classes from the Java libraries.

Jython包括大部分属于Python语言的模块。它还可以导入并使用Java库中的任何类。

Let’s see a quick “Hello, World!” example:

让我们看看一个快速的 “你好,世界!”的例子。

print "Hello, world!"

7.2. JRuby

7.2 JRuby

JRuby is an implementation of the Ruby programming language to run on the Java Virtual Machine.

JRubyRuby编程语言的实现,可在Java虚拟机上运行。

The JRuby language is high performance and multi-threaded, with a vast variety of available libraries from Java and Ruby. Also, it combines features from both languages like object-oriented programming and duck-typing.

JRuby语言是高性能和多线程的,有大量来自Java和Ruby的可用库。同时,它结合了这两种语言的特点,如面向对象编程和鸭子类型。

Let’s print “Hello, World!” in JRuby:

让我们在JRuby中打印 “你好,世界!”。

require "java"

stringHello= "Hello World"
puts "#{stringHello.to_s}"

8. Conclusion

8.结论

In this article, we’ve studied many popular JVM languages along with basic code examples. These languages implement various programming paradigms like object-oriented, functional, static typing, dynamic typing.

在这篇文章中,我们已经研究了许多流行的JVM语言以及基本的代码例子。这些语言实现了各种编程范式,如面向对象、函数式、静态类型、动态类型。

So far it shows that even with the JVM dating back to 1995, it still stands as a highly relevant and compelling platform for modern-day programming languages.

到目前为止,它表明即使JVM可以追溯到1995年,它仍然是现代编程语言的一个高度相关和引人注目的平台。