In Java, is it possible to cast to void (not Void)?

你说的曾经没有我的故事 提交于 2019-12-23 14:01:35

问题


Is there anything I can put in X, to make the follow work:

Object o = (void) X;

回答1:


void is notionally a primitive. (though most would disagree it is even that I suspect) You cannot cast an object to it.

The closest you can come to this is an InvocationHandler can return null for a void method and a void method invoke()ed via reflection will return null.




回答2:


Java is not C++. In Java, void is not a type, it is a placeholder that means "no return value".




回答3:


No. You can set x = null; if you wish though?




回答4:


Maybe this discussion might be of interest: Uses for the Java Void Reference Type?, it covers about everything you can do with Void and what it's good for.



来源:https://stackoverflow.com/questions/4830843/in-java-is-it-possible-to-cast-to-void-not-void

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!