Skip to main content

Uses for the Java Void Reference Type?



There is a Java Void -- uppercase V-- reference type . The only situation I have ever seen it used is to parameterize Callable s







final Callable<Void> callable = new Callable<Void>() {

public Void call() {

foobar();

return null;

}

};







Are there any other uses for the Java Void reference type? Can it ever be assigned anything other than null ? If yes, do you have examples?


Comments

  1. Void has become convention for a generic argument that you are not interested in. There is no reason why you should use any other non-instantiable type, such as System.

    It is also often used in for example Map values (although Collections.newSetFromMap uses Boolean as maps don't have to accept null values) and java.security.PrivilegedAction.

    I wrote a weblog entry on Void a few years back.

    ReplyDelete
  2. Given that there are no public constructors, I would say it can't be assigned anything other than null. I've only used it as a placeholder for "I don't need to use this generic parameter," as your example shows.

    It could also be used in reflection, from what its Javadoc says:


    The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.

    ReplyDelete
  3. Future<Void> works like charm. :)

    ReplyDelete
  4. You can create instance of Void using reflections, but they are not useful for anything. Void is a way to indicate a generic method returns nothing.

    ReplyDelete
  5. All the primitive wrapper classes (Integer, Byte, Boolean, Double, etc.) contain a reference to the corresponding primitive class in a static TYPE field, for example:

    Integer.TYPE == int.class
    Byte.TYPE == byte.class
    Boolean.TYPE == boolean.class
    Double.TYPE == double.class


    Void was initially created as somewhere to put a reference to the void type:

    Void.TYPE == void.class


    However, you don't really gain anything by using Void.TYPE. When you use void.class it's much clearer that you're doing something with the void type.

    As an aside, the last time I tried it, BeanShell didn't recognise void.class, so you have to use Void.TYPE there.

    ReplyDelete
  6. Before generics, it was created for the reflection API, to hold TYPE returned by Method.getReturnType() for a void method, corresponding to the other primitive type classes.

    EDIT: From the JavaDoc of Void: "The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void". Prior to Generics, I am aware of no use other than reflection.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.