Java虚拟机是否允许对返回类型进行重载? [英] Does Java virtual machine allow overloading on return type?

查看:69
本文介绍了Java虚拟机是否允许对返回类型进行重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了演示文稿.

幻灯片编号:26引用

Java language does not allow overloading on return type

Java Virtual machine does allow overloading on return type

这些陈述正确吗?如果两个语句都正确,那么如何使代码可编译,以便jvm运行代码?

我对此主题有一个SE问题:

Java-为什么没有基于返回类型的方法重载?

先谢谢了.

解决方案

这些陈述完全正确.

请记住,Java是两件事-一是语言,其二是虚拟机.虽然将语言限制为不允许基于类型的方法重载使Java成为一种更简单的语言,但JVM仍可以允许这样做以使其更强大.

作为一种语言,Java具有一种编译器,该编译器执行一些规则,这些规则使Java成为比允许这样做的语言更简单,更轻松的编程语言.为此,它限制了您只能在Java语言本身中执行的操作.在JVM上运行诸如Scala或Ruby之类的东西需要不同的规则和功能,并且在此级别上,重要的是JVM必须具有足够的灵活性,以使JVM取得如此巨大的成功,以至于在许多平台和设备上都可以找到它. >

在一种可能通过返回类型重载的语言中,这将很容易出错,并且不支持该功能的决定是故意使Java成为不那么容易出错的编程语言.编译器将如何知道您打算调用哪个函数?

另一方面,JVM是一个高度底层的,高度优化的虚拟机,可以运行字节码而不是Java.因此,以这种方式限制JVM是不明智的,因为它应该能够运行根本不是从Java生成的字节码.

另一个示例是多重继承,这在Java中不可用,但是没有什么可以阻止您编写支持多重继承并将其编译为字节码的语言.这将使您的语言更难以使用,并且可能更容易出错,但是如果您需要该功能,则JVM不会阻止您.

I have gone through this presentation.

Slide No:26 quote that

Java language does not allow overloading on return type

Java Virtual machine does allow overloading on return type

Are these statements true? If both statements are true, how to make the code compilable so that jvm run the code?

I have one SE question on this topic :

Java - why no return type based method overloading?

Thanks in advance.

解决方案

These statements are perfectly true.

Remember that Java is two things - one,a language and two, a virtual machine. While restricting the language to not allow type based method overloading makes Java a simpler language to use, the JVM can still allow this to be done to make it more powerful.

As a language, Java has a compiler that enforces the rules that make Java a simpler, easier programming language than one that does allow this. To do this, it restricts what you can do, but only in the Java language itself. Running something like Scala or Ruby on the JVM requires different rules and features, and at this level it is important that the JVM allows the flexibility that has made the JVM such a big success that it is found on so many platforms and devices.

In a language where overloading by return type is possible, it would be very error prone and the decision to not support that feature was deliberate to make Java a less error-prone programming language. How would the compiler know which function you intended to call?

The JVM on the other hand is a low-level, highly optimised virtual machine that exists to run bytecode, not Java. It is therefore not wise to restrict the JVM in this fashion as it should be able to run bytecode not generated from Java at all.

Another example would be multiple inheritance, which is not available in Java, yet nothing prevents you to write a language that supports multiple inheritance and have it compile to bytecode. It would make your language more difficult to use and potentially more error prone, but if you require that feature, the JVM will not stop you.

这篇关于Java虚拟机是否允许对返回类型进行重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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