简体   繁体   中英

Whats the difference between main() and main(String args[])

I am a biggner in java. I am going well with java. The problem is when we declare main function in java as main(String args). I am learning with bluej. It worked fine if I just write main(). So what's the difference between both.

public static void main(String[] args is the entry point (which can be final or not, doesn't matter) that the java tool and standard IDEs and such look for in the main class of a Java application. If you don't include the parameter declaration (the String[] args ), the signature doesn't match the expectation of the java tool and so may not work.

main() will compile , because it's just a method, but won't work with the java tool and other tools following its conventions.

If BlueJ allows you to leave off the parameter declaration, that's behavior specific to the BlueJ tool.

So for instance, this compiles just fine:

public class Example {
    public static void main() {
        System.out.println("Hi");
    }
}

It compiles to an Example class with a method called main . But if you try to run that via the java tool:

$ java Example
Error: Main method not found in class Example, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

To make it compatible with the java tool, you need the parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM