我们如何在JVM上拥有一种动态类型的语言? [英] How can we have a dynamically typed language over JVM?

查看:168
本文介绍了我们如何在JVM上拥有一种动态类型的语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有JythonJRubyGroovy,它们是动态键入的,并且在JVM上运行.我了解这些语言会编译为字节码.

We have Jython, JRuby, Groovy which are dynamically typed and runs over JVM. I understand that these languages compile to bytecode.

Scala之类的其他语言也支持类型推断,显然,编译器会为我们推断类型.

Other languages such as Scala supports type inference, and its clear that the compiler infers the type for us.

但是Java是一种静态语言,可以编译为字节码,这是否意味着字节码支持动态类型?

But Java is a static language and it compiles to bytecode, does this mean bytecode supports dynamic typing?

例如在Java中,我们需要在编译时声明变量类型,并且永远不能更改它.但是在Python的情况下,我们不必声明类型,但是可以在运行时将任何类型的值分配给同一变量.

For e.g. In Java we need to declare the variable type at compile time, and can never change it. But in case of Python we dont have to declare a type, but we can assign any type of value to the same variable during run time.

动态键入如何在静态语言上工作?

How does the dynamic typing work over a static language?

推荐答案

但是Java是一种静态语言,它可以编译为字节码,这是否意味着字节码支持动态类型化?

But Java is a static language and it compiles to bytecode, does this mean bytecode supports dynamic typing?

是的,这确实意味着.

您看到Java不是完全静态类型的语言.每当将对象从类型转换为子类型时,JVM都会执行动态(运行时)类型检查,以检查该对象是否确实是该子类型的实例.使用instanceof是动态类型检查的另一个示例.

You see Java is not a completely statically typed language. Whenever you cast an object from a type to a subtype, the JVM performs a dynamic (runtime) typecheck to check that the object really is an instance of the subtype. Using instanceof is another example of dynamic type checking.

在使用反射API时,甚至在使用泛型时,也会在后台使用动态类型检查.

Dynamic type checking is also used under the covers when you use the reflection APIs, and even when you use generics.

动态键入如何在静态语言上工作?

How does the dynamic typing work over a static language?

它是一种纯静态的类型检查语言,而实际上不是.例如,Pascal是一种具有(完全)静态类型的强类型语言.但是大多数现代编程语言至少支持某种程度的运行时类型检查.而且许多动态类型化的语言具有可选的静态类型,或者具有使用类型推断来拾取与类型相关的错误的开发人员工具.

It it is a purely statically type-checked language then it doesn't. For instance, Pascal is a strongly typed language with (purely) static typing. But most modern programming languages support at least some level of runtime type checking. And many dynamically typed languages have either optional static typing, or developer tools that use type inferencing to pick up type-related errors.

顺便说一句,语言既可以是静态类型,也可以使用类型推断.应该将类型推断视为显式类型声明的替代方法,而不是静态类型的替代方法.

Incidentally, a language can be both statically typed and use type inferencing. Type inference should be viewed as an alternative to explicit type declarations, not an as alternative to static typing.

这篇关于我们如何在JVM上拥有一种动态类型的语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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