Why it is impossible to use Void as return type for the main method

So, I wanted to test the Void type, then I wrote this simple program:

 package ehsan; public class NumTest { public static Void main(String[] args) { System.out.println("Hello, World!"); return null; /* The compiler forced me to do so. I just can't realize what is the point in returning in Void type!? */ } } 

So, now that I want to compile, the compiler complains:

 main method must return a value of type void 

Why does the compiler not see that I am not returning anything and using Void ?

+5
source share
4 answers

You should use a void (lowercase v ) non void object. void object will not receive autoboxing, for example, for example. int / Integer , see Java Specification for a list of autoboxing objects.

void not a wrapper for void , it is just an object with a very similar name, so it can be used in those places where you need to specify the return type (for example, Callable<T> ), it is only for documentation purposes and to bypass some types of return types common classes.

The second use case is reflected (if you want to check the return value of the void function, you will get Void.TYPE ).

The correct line is:

 public static void main(String[] args) 
+4
source

Void is a type of class, so the compiler expects you to return a value to it. You must use void with a lowercase letter.

The reason for your error:

The main method should return a value of type void

due to the fact that the main method should always return void - this is the java keyword, not the class type.

+2
source

You should use void instead of void

Like this:

 public static void main(String[] args) 
+1
source

Typo Error.

This void not void

It happens that Void is a class in java. From the documents

The Void class is an uninteresting placeholder class for storing a reference to a class object that represents the void Java keyword.

+1
source

All Articles