为什么我们需要其他JVM语言 [英] Why do we need other JVM languages

查看:97
本文介绍了为什么我们需要其他JVM语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处,我看到除了语言之外还有很多语言在JVM上运行的Java.我对JVM中运行的其他语言的整个概念有些困惑.所以:

为JVM使用其他语言有什么优势?

为JVM编写语言/编译器需要什么(概括而言)?

您如何在JVM中以某种语言(不是Java)编写/编译/运行代码?


编辑:在接受的答案中回答了3个后续问题(最初为评论).为了清晰起见,此处将其重印:

用JPython编写的应用程序将如何与Java应用程序交互?

此外,该JPython应用程序可以使用任何JDK函数/对象吗?

如果它是Jaskell代码,那它是一种功能语言会不会使其与JDK不兼容吗?

解决方案

分别解决您的三个问题:

为JVM使用其他语言有什么优势?

这里有两个因素. (1)为什么要为JVM使用Java以外的其他语言,以及(2)为什么要在JVM上运行另一种语言而不是其他运行时?

  1. 其他语言可以满足其他需求.例如,Java不内置对闭包的内置支持.很有用.
  2. 在JVM上运行的语言与在JVM上运行的任何其他语言的字节码兼容,这意味着用一种语言编写的代码可以与用另一种语言编写的库进行交互.

(从高级的角度来说)为JVM编写语言/编译器需要什么?

JVM读取字节码(.class)文件以获得其需要执行的指令.因此,要在JVM上运行的任何语言都需要编译为遵守 Scala ,它可以在JVM上完美运行. /p>

回答您的后续问题:

用JPython编写的应用程序如何与Java应用程序交互?

这取决于实现方案选择弥合语言鸿沟.在您的示例中, Jython项目具有执行此操作的简单方法(

I see here that there are a load of languages aside from Java that run on the JVM. I'm a bit confused about the whole concept of other languages running in the JVM. So:

What is the advantage in having other languages for the JVM?

What is required (in high level terms) to write a language/compiler for the JVM?

How do you write/compile/run code in a language (other than Java) in the JVM?


EDIT: There were 3 follow up questions (originally comments) that were answered in the accepted answer. They are reprinted here for legibility:

How would an app written in, say, JPython, interact with a Java app?

Also, Can that JPython application use any of the JDK functions/objects??

What if it was Jaskell code, would the fact that it is a functional language not make it incompatible with the JDK?

解决方案

To address your three questions separately:

What is the advantage in having other languages for the JVM?

There are two factors here. (1) Why have a language other than Java for the JVM, and (2) why have another language run on the JVM, instead of a different runtime?

  1. Other languages can satisfy other needs. For example, Java has no built-in support for closures, a feature that is often very useful.
  2. A language that runs on the JVM is bytecode compatible with any other language that runs on the JVM, meaning that code written in one language can interact with a library written in another language.

What is required (in high level terms) to write a language/compiler for the JVM?

The JVM reads bytecode (.class) files to obtain the instructions it needs to perform. Thus any language that is to be run on the JVM needs to be compiled to bytecode adhering to the Sun specification. This process is similar to compiling to native code, except that instead of compiling to instructions understood by the CPU, the code is compiled to instructions that are interpreted by the JVM.

How do you write/compile/run code in a language (other than Java) in the JVM?

Very much in the same way you write/compile/run code in Java. To get your feet wet, I'd recommend looking at Scala, which runs flawlessly on the JVM.

Answering your follow up questions:

How would an app written in, say, JPython, interact with a Java app?

This depends on the implementation's choice of bridging the language gap. In your example, Jython project has a straightforward means of doing this (see here):

from java.net import URL
u = URL('http://jython.org')

Also, can that JPython application use any of the JDK functions/objects?

Yes, see above.

What if it was Jaskell code, would the fact that it is a functional language not make it incompatible with the JDK?

No. Scala (link above) for example implements functional features while maintaining compatibility with Java. For example:

object Timer {
  def oncePerSecond(callback: () => unit) {
    while (true) { callback(); Thread sleep 1000 }
  }
  def timeFlies() {
    println("time flies like an arrow...")
  }
  def main(args: Array[String]) {
    oncePerSecond(timeFlies)
  }
}

这篇关于为什么我们需要其他JVM语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆