Autoboxing and emptiness

Note that Integer.TYPE and Integer.class are Class<Integer> , Double.TYPE and Double.class are equal to Class<Double> and how you use autoboxing to convert between int and Integer , as well as double and double . The question is, is this true for void : Void.TYPE and Void.class both Void.class , but can you "convert" between void and void ?

In other words, suppose you have this interface:

 public interface Foo<T> { public T doSomething(); } 

A class that implements Foo<Integer> can return an int in its doSomething() implementation when an int is marked. Similarly for Foo<Double> return double . So, for a Foo<Void> : since a valid void valid, it is null (unless you make a weird reflection that is rarely justified), does that mean you can omit the required return null , is void boxing effective?

+4
source share
1 answer

Does this mean that you can omit the required null return by effectively boxing the Void?

Nope. Of course, this is easy to check for yourself:

 class Test { public Void foo() { } } 

Compilation gives:

 Error: Test.java:3: missing return statement } 

... or you can just read JLS when autoboxing ( section 5.1.7 ), which does not mention Void anywhere.

+10
source

Source: https://habr.com/ru/post/1433939/


All Articles