Statement about main:
1.java virtual machine call, so it must be public;
2. When there is a main() method in a class, executing the command "java class name" will start the virtual machine to execute the main method in the class.
Since the JVM is running the Java application, the main method is called first. The object of this class is not instantiated when called, but is called directly by the class name, so it needs to be restricted to public static. (class name. main())
3. jvm has a limit, there can be no return value, so the return value type is void; (it seems that because you can't get the return value, you just don't want it)
4. The main function can only appear in the public class, which is the public class.
In addition to this, args[] in the main method is used for what, I have been curious before. Later, I found out that the information was known:
The string parameter array in the main() method is used to receive command line input parameters. The parameters of the command line are separated by spaces.
The parameter args provides a means for the program user to interact with the program in the command line state.
For example: javac Test1.java
java Test1 60 50 40
Next iterate over the args[] array,
for(int i=0;i<args.length;i++){
System.out.println(args[i]);
}
The result is 60 50 40
There is another question: Why is the parameter passed in Application.launch() in Javafx args?
I encountered this when I first learned javafx:
public static void main(String[] args) {
launch(args);
}
In fact, the purpose of this biography is
Used to pass arguments from the console, just like the main function main(String[] args), args is the argument passed by the console.
Sometimes you need to pass this parameter when you start it, such as executing the command in the console java Demo abc
, abc will be passed to the main function as a parameter args.
We know that passing an array name is actually a pass address. Application.launch() in Javafx is static
public static void launch(String... args) {
...........
...........
}
Then there is a problem, we often use eclipse or idea tools to write java programs and then run, instead of javac java such dos window form, then how to pass parameters to the args array.
In eclipse, you can select the running parameters at runtime: Click Run -> Run Configurations -> Java Application, then select the class corresponding to the project Test1, then click Arguments, enter the string "60 50 40" as the program in the program arguments box. Parameters, then click Apply -> Run
Today, when reviewing Java foundation, a problem that has been ignored before, is the String in the main method [] Args is a bar. Now answers to Java Core Technology. Command line parameters Each Java...
Error: Can't find the main method in class fH.atest, please define the main method as: public static void main (String [] args) Otherwise the JavaFX application class must extend javafx.Application.Ap...
I recently encountered this problem when writing java programs using eclipse: Error The main method cannot be found in the class. Please define the main method as public static void main(String[] args...
First step to create a Java file Second step Enter Javac Temp.java in the command line Enter Java Temp Hello World after compiling hello world ARGS is used to receive a string array of command line in...
Have the following code The directory structure is D:/src/com/company/Sleep.java The first step: under the company javac -encoding utf-8 Sleep.java Step 2: Under src java com.company.Sleep a45 8 7 or ...
Example of main method: One, run the java program 1. Compile: javac file.java (when the java file contains Chinese, use "javac -encoding UTF-8 file.java" command) 2. Run: java file Expansion...
The function of String[] args; is to receive the parameters passed into the main method before the main method runs (args is the abbreviation of arguments): Code test: Let's traverse this array: Pass ...
For the original text, please refer to:Click to open the link Analyze the following code: We use the command to compile and run We can see the difference. Summary: The string array parameter args prov...
When I first started studying, I was very confused about what the Main method String [] ARGS did, and I never saw it. Today, I only know what to do with the basic knowledge chapter of Java core techno...
A class can only run if it contains an entry. This entry tells the system where to start the program. The format of the entry is fixed, for example:...