How can we have a dynamically typed language over

2020-06-09 07:37发布

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

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

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

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?

3条回答
2楼-- · 2020-06-09 08:10

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.

查看更多
放我归山
3楼-- · 2020-06-09 08:25

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.

查看更多
地球回转人心会变
4楼-- · 2020-06-09 08:34

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.

查看更多
登录 后发表回答