0

In the following code when i chaged the return type then it did not give the compiler error just throw runtime exception.Is it not the syntex error to give the wrong return type of long instead of void.

class Jfilestr {
    public static long main(String[] args) {
        System.out.println("Hello");      
        return 10L;

        //It will give the runtime exception.
        /*Error: Main method must return a value of type void in class Jfilestr, please
        define the main method as:
         public static void main(String[] args)*/
    }
}
explv
  • 2,709
  • 10
  • 17

1 Answers1

0

Your problem is that

public static void main(String[] args) {}

plays a special role in Java. It is the entry point when you start a Java application. Technically, you can create the method you described, but won't function as an entry point. This happens often enough that the runtime detects the error and tells you what you did.

You can't return a value from the entry point method.

Steve11235
  • 2,849
  • 1
  • 17
  • 18
  • 1
    I see what you are getting at. The java call specified the class, and it is smart enough to detect an invalid main. I'll update my answer. Thanks. – Steve11235 May 11 '18 at 16:03