java 主要方法是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15962535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does the main method work?
提问by Throsby
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. 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?
我是一些破碎教学的产物,我需要一些帮助。我知道在 Java 中有一个叫做“main 方法”的东西,我相信其他编程语言。我知道您需要一个来运行您的代码,但是您如何使用它来运行您的代码?它有什么作用,你需要把它放进去什么才能让你的代码运行?
I know it should look something like this. But almost nothing more.
我知道它应该看起来像这样。但几乎没有更多。
static void main(String[] args){
}
回答by Diana E
Breaking this down, point-by-point, for the general case:
对于一般情况,逐点分解:
- All Java applications begin processing with a
main()
method; - Each statement in the
main
executes in order until the end ofmain
is reached -- this is when your program terminates; - What does
static
mean?static
means that you don't have to instantiate a class to call the method; String[]
args is an array ofString
objects. 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]
;public
means that the method can be called by any object.
- 所有 Java 应用程序都从一个
main()
方法开始处理; - 中的每条语句都按
main
顺序执行,直到到达结束main
——这是你的程序终止的时候; - 什么
static
意思?static
意味着您不必实例化一个类来调用该方法; String[]
args 是一个String
对象数组。如果要在命令行上运行程序,则可以将参数作为参数传递。然后,这些参数可被访问,你会访问元素的数组:args[0]...args[n]
;public
意味着该方法可以被任何对象调用。
回答by Neil Locketz
its the entry point for any java program, it does whatever you tell it to do. all you need to do is declare it in one of your source java files and the compiler will find it.
它是任何 Java 程序的入口点,它会执行您告诉它执行的任何操作。您需要做的就是在源 java 文件之一中声明它,编译器会找到它。
回答by SomeShinyObject
Summary
概括
void
means the method returns no value. String[] args
represents a string of arguments in an Array type that are passed to the program. main
is the single point of entry in to most Java programs.
void
意味着该方法不返回任何值。String[] args
表示传递给程序的 Array 类型的参数字符串。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. It just so happens that the main
method in Java has to return void. In C or C++, the main method can return
an int
and usually this indicates the code status of the finished program. An int
value of 0
is the standard for a successful completion.
粉饰,为什么是public static void...
(你不会建立一个没有门把手上的门),方法(等价的功能(或其他语言的潜艇))在Java中有一个返回类型。碰巧的是main
,Java中的方法必须返回 void。在C或C ++中,main方法可以return
一个int
,并且通常这表示成品程序的代码状态。一个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. Usually these are used to modify a default behavior or for flags used in short command line executable applications.
String[] args
是参数的字符串数组,传递给主函数,通常供特定应用程序使用。通常这些用于修改默认行为或用于短命令行可执行应用程序中使用的标志。
main
just means to the JVM, start here! Same name in C and C++ (and probably more).
main
只是对 JVM 的意思,从这里开始!C 和 C++ 中的同名(可能更多)。
回答by MadProgrammer
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
看一下main方法
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.
}
}
回答by Vincent Jia
You should have a main classand 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.
这使得代码运行活跃。
回答by Marshall Tigerus
when you execute your class, anything in the main method runs.
当你执行你的类时,main 方法中的任何东西都会运行。