Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

void

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

Object o = (void) X;
like image 367
John Assymptoth Avatar asked Dec 22 '22 18:12

John Assymptoth


2 Answers

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.

like image 175
Peter Lawrey Avatar answered Dec 24 '22 07:12

Peter Lawrey


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

like image 39
DwB Avatar answered Dec 24 '22 08:12

DwB