问题
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