简体   繁体   English

主要方法如何工作?

[英]How does the main method work?

I'm the product of some broken teaching and I need some help. 我是一些破碎教学的产物,我需要一些帮助。 I know that there is this thing called the "main method" in Java and I'm sure other programming languages. 我知道Java中有一个叫做“主要方法”的东西,我确信其他的编程语言。 I know that you need one to make your code run, but how do you use it to make your run? 我知道你需要一个让你的代码运行,但你如何使用它来运行? What does it do and what do you need to have it put in it to make your code run? 它做了什么,你需要把它放进去让你的代码运行?

I know it should look something like this. 我知道它看起来应该是这样的。 But almost nothing more. 但几乎没有。

static void main(String[] args){

}

Breaking this down, point-by-point, for the general case: 对于一般情况,逐点打破这一点:

  1. All Java applications begin processing with a main() method; 所有Java应用程序都开始使用main()方法进行处理;
  2. Each statement in the main executes in order until the end of main is reached -- this is when your program terminates; main中的每个语句按顺序执行,直到到达main的末尾 - 这是程序终止时;
  3. What does static mean? static是什么意思? static means that you don't have to instantiate a class to call the method; static意味着您不必实例化类来调用该方法;
  4. String[] args is an array of String objects. String[] args是一个String对象数组。 If you were to run your program on the command line, you could pass in parameters as arguments. 如果要在命令行上运行程序,则可以将参数作为参数传递。 These parameters can then be accessed as you would access elements in an array: args[0]...args[n] ; 然后可以访问这些参数,就像访问数组中的元素一样: args[0]...args[n] ;
  5. public means that the method can be called by any object. public意味着该方法可以被任何对象调用。

its the entry point for any java program, it does whatever you tell it to do. 它是任何java程序的入口点,无论你告诉它做什么。 all you need to do is declare it in one of your source java files and the compiler will find it. 所有你需要做的就是在你的一个源java文件中声明它,编译器会找到它。

Firstly it should be public static void main(String[] args){...} . 首先它应该是public static void main(String[] args){...}

It must be public 它必须是public

Take a look at The main method 看看主要方法

The JVM will look for this method signature when it runs you class... JVM将在您运行类时查找此方法签名...

java helloWorld.HelloWorld

It represents the entry point for your application. 它代表了您的应用程序的入口点。 You should put all the required initialization code here that is required to get your application running. 您应该将所有必需的初始化代码放在此处,以使您的应用程序运行。

The following is a simple example (which can be executed with the command from above) 以下是一个简单的例子(可以使用上面的命令执行)

package helloWorld;

public class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

Summary 摘要

void means the method returns no value. void表示该方法不返回任何值。 String[] args represents a string of arguments in an Array type that are passed to the program. String[] args表示传递给程序的Array类型中的一个参数字符串。 main is the single point of entry in to most Java programs. main是大多数Java程序的单一入口点。

Extended 扩展

Glossing over why it should be public static void... (You wouldn't build a door without a doorknob), methods (equivalent of functions (or subs) in other languages) in Java have a return type. 为什么它应该是public static void... (你不会在没有doorknob的情况下构建一个门),Java中的方法(相当于其他语言中的函数(或subs))具有返回类型。 It just so happens that the main method in Java has to return void. 碰巧Java中的main方法必须返回void。 In C or C++, the main method can return an int and usually this indicates the code status of the finished program. 在C或C ++中,main方法可以return一个int ,通常这表示已完成程序的代码状态。 An int value of 0 is the standard for a successful completion. int值为0是成功完成的标准。

To get the same effect in Java, we use System.exit(intValue) 为了在Java中获得相同的效果,我们使用System.exit(intValue)

String[] args is a string array of arguments, passed to the main function, usually for specific application use. String[] args是一个String[] args的字符串数组,传递给main函数,通常用于特定的应用程序。 Usually these are used to modify a default behavior or for flags used in short command line executable applications. 通常这些用于修改默认行为或短命令行可执行应用程序中使用的标志。

main just means to the JVM, start here! main只是意味着JVM,从这里开始! Same name in C and C++ (and probably more). C和C ++中的名称相同(可能更多)。

当你执行你的类时,main方法中的任何东西都会运行。

You should have a main class and a main method . 你应该有一个主类和一个主方法 And if you want to find something in the console, you need have one output command at least in the method, like : 如果你想在控制台中找到一些东西,你需要至少在方法中有一个输出命令,如:

System.out.println("Hello World!"); System.out.println(“Hello World!”);

This makes the code running lively . 这使代码运行生动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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