5

So I wanted to test 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 when I want to compile, the compiler complains :

main method must return a value of type void

Why can't the compiler see that I'm returning nothing and am using Void?

  • 5
    `Void` and `void` are not the same. – Eran Jul 07 '16 at 07:15
  • 3
    Because the standard defines the signature of the main method to be `public static void main(String... args)`. Autoboxing/unboxing has nothing to do with method-signatures. You can't auto-box a signature. – tkausl Jul 07 '16 at 07:18
  • 3
    Void is not a wrapper for void – Eran Jul 07 '16 at 07:18
  • 1
    Yup, Java has some messy corners. This is one of them. Get over it and move on. (Nice question by the way though). – Bathsheba Jul 07 '16 at 07:21

4 Answers4

4

You should use void (lowercase v) not Void object. Void object is not going to get autoboxing like e.g. int/Integer, see Java Language Specification for a list of autoboxing objects.

Void is not a wrapper for void, it is just an object that has a very similar name so it can be used in places where you need to specify a return type (like e.g. Callable<T>), it is just for documentation purposes and to workaround some generic classes return types.

Second use case is in reflection (when you want to check the return value of a void function, you will get Void.TYPE).

Correct line is:

public static void main(String[] args)
Krzysztof Krasoń
  • 23,505
  • 14
  • 77
  • 102
  • So the only use of `Void` is in specifying generic types? ( Sorry for my newbish questions :) –  Jul 07 '16 at 07:21
  • Usually, there are some other use cases related to reflection, as it is written in `Void` docs: *The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.* – Krzysztof Krasoń Jul 07 '16 at 07:23
2

Void is a class type, so compiler expects you to return a value for it. You should usevoid with lower case letter.

The cause of your error:

main method must return a value of type void

is due to the rule that main method should always return void - which is a java keyword not a class type.

marcinj
  • 44,446
  • 9
  • 70
  • 91
1

You have to use void instead of Void

Like this:

public static void main(String[] args)
Blobonat
  • 1,195
  • 1
  • 13
  • 29
1

Typo error.

It is void not Void

It is happened that Void is a class in java. From Docs

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

Bhargav Kumar R
  • 2,012
  • 3
  • 20
  • 38