English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Can the main method in Java return a value?

public static void main(String args[]) - Is the entry point of Java programs, every time you run a program, JVM will search for the main method and start executing its content. If such a method is not found, the program will execute successfully, but when you run the program, it will generate an error.
In fact, you should use public static as the modifier to declare the main method, void return type, and string parameter. If you make any changes, JVM will not consider it as the entry point method and will prompt an error at runtime.
Therefore, you cannot change the return type of the main method from void, and you cannot return any value from a void type method.

Example

public class Sample{
   public static void main(String args[]){
      System.out.println("Main method content");
      return 20;
   }
}

Output Result

Sample.java:4: error: incompatible types: unexpected return value
   return 20;
          ^
1 error

Therefore, you cannot return any value from main.