How can we have a dynamically typed language over JVM?

前端 未结 3 683
花落未央
花落未央 2021-02-13 22:46

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

相关标签:
3条回答
  • 2021-02-13 22:49

    As others have pointed out, a dynamic language simply means that some (and often all) type checking is done entirely at runtime. You can build very dynamic languages even in very statically typed (or even nearly untyped (x86 machine code)) environments.

    Java has also been adding more and more native support for dynamic languages. Sun published a very good overview of what this means and how it helps dynamic languages perform well and feel at home on the JVM.

    0 讨论(0)
  • 2021-02-13 22:58

    You're confusing language and architecture. In general the architecture knows nothing about types. Dynamic typing is when objects carry type information with them. You could think of dynamically typed language as Java with only type 'Object' and a lot of 'instanceof' checks behind your back.

    0 讨论(0)
  • 2021-02-13 23:10

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

    Yes it does mean that.

    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.

    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?

    If 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.

    0 讨论(0)
提交回复
热议问题