Why do we need other JVM languages

前端 未结 17 1544
滥情空心
滥情空心 2021-02-02 10:30

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:

相关标签:
17条回答
  • 2021-02-02 11:27

    Java has accumulated a massive user base over seven major versions (from 1.0 to 1.6). Its capability to evolve is limited by the need to preserve backwards compatibility for the uncountable millions of lines of Java code running in production.

    This is a problem because Java needs to evolve to:

    • compete with newer programming languages that have learned from Java's successes and failures.
    • incorporate new advances in programming language design.
    • allow users to take full advantage of advances in hardware - e.g. multi-core processors.
    • fix some cutting edge ideas that introduced unexpected problems (e.g. checked exceptions, generics).

    The requirement for backwards compatibility is a barrier to staying competitive.

    If you compare Java to C#, Java has the advantage in mature, production ready libraries and frameworks, and a disadvantage in terms of language features and rate of increase in market share. This is what you would expect from comparing two successful languages that are one generation apart.

    Any new language has the same advantage and disadvantage that C# has compared to Java to an extreme degree. One way of maximizing the advantage in terms of language features, and minimizing the disadvantage in terms of mature libraries and frameworks is to build the language for an existing virtual machine and make it interoperable with code written for that virtual machine. This is the reason behind the modest success of Groovy and Clojure; and the excitement around Scala. Without the JVM these languages could only ever have occupied a tiny niche in a very specialized market segment, whereas with the JVM they occupy a significant niche in the mainstream.

    0 讨论(0)
  • 2021-02-02 11:27

    They do it to keep up with .Net. .Net allows C#, VB, J# (formerly), F#, Python, Ruby (coming soon), and c++. I'm probably missing some. Probably the big one in there is Python, for the scripting people.

    0 讨论(0)
  • 2021-02-02 11:29

    I would answer, “because Java sucks” but then again, perhaps that's too obvious … ;-)

    0 讨论(0)
  • 2021-02-02 11:29

    To an extent it is probably an 'Arms Race' against the .NET CLR.

    But I think there are also genuine reasons for introducing new languages to the JVM, particularly when they will be run 'in parallel', you can use the right language for the right job, a scripting language like Groovy may be exactly what you need for your page presentation, whereas regular old Java is better for your business logic.

    I'm going to leave someone more qualified to talk about what is required to write a new language/compiler.

    As for how to writing code, you do it in notepad/vi as usual! (or use a development tool that supports the language if you want to do it the easy way.) Compiling will require a special compiler for the language that will interpret and compile it into bytecode.

    Since java also produces bytecode technically you don't need to do anything special to run it.

    0 讨论(0)
  • 2021-02-02 11:30

    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)
      }
    }
    
    0 讨论(0)
提交回复
热议问题