简体   繁体   English

Java public static main()

[英]Java public static main()

I am learning Java, theres one thing I do not understand.. 我正在学习Java,有一件事我不明白..

in the main routine: 在主程序中:

public static void main(String[] args) {

I think I pretty much understand this, in the language I know, I think it would be like this: 我想我非常理解这一点,用我所知的语言,我认为它会是这样的:

public static function main(args:String):void {

The first thing I do not understand is what are the 2 brackets [] for in String[]? 我不明白的第一件事是String []中的2个括号[]是什么? Also the second thing I am wondering, is if this is the first function that will be called (and called by something outside the program), will there ever actually be a parameter passed? 另外我想知道的第二件事是,如果这是第一个被调用的函数(并且被程序之外的东西调用),那么实际上是否会传递参数?

Thanks. 谢谢。

The arguments to main are the options you pass into Java from the command line, passed in as an array. main的参数是从命令行传入Java的选项,作为数组传入。 So for example : 例如:

java MyProgram foo bar zoo

takes three arguments, namely, foo, bar, and zoo 有三个参数,即foo,bar和zoo

foo is args[0], bar is args[1], and zoo is args[2]. foo是args [0],bar是args [1],动物园是args [2]。

Brackets mean array . 括号表示阵列 Eg String[] is an array of strings. 例如String[]是一个字符串数组。 The main() -function is the first function called in your program. main()函数是程序中调用的第一个函数。 It gets called by the JVM . 它由JVM调用。

The values in String[] args are the parameters passed on the command line. String[] args中的值是在命令行上传递的参数。

If you call a Java program (main class: FooBar in package foo.bar ) like that: 如果你调用一个Java程序(主类: FooBar包中的foo.bar ):

java foo.bar.FooBar foo bar buz

then, args will like if you built it like that: 那么, args会喜欢你是这样构建的:

String[] args = new String[3];
args[0] = "foo";
args[1] = "bar";
args[2] = "buz";

That is possibly worth reading: A Closer Look at the "Hello World" Application 这可能值得一读: 仔细研究“Hello World”应用程序

The brackets mean that it's an array of Strings. 括号表示它是一个字符串数组。 And there can be parameters, eg from the command line when your start your application. 并且可以有参数,例如,当您启动应用程序时从命令行。

It means that you'll get an array of strings. 这意味着你将得到一个字符串数组。 They may be passed via command line 它们可以通过命令行传递

[] stands for array for example String x = "some value"; []代表数组,例如String x =“some value”; String[] x = {"value 1","value 2","value 3"}; String [] x = {“value 1”,“value 2”,“value 3”};

so in second case x[0] gives "value 1". 所以在第二种情况下,x [0]给出“值1”。 It is basically an array of strings. 它基本上是一个字符串数组。 Second part is who will call the function? 第二部分是谁将调用该函数? Well this method signature is entry signature and whenever you try to invoke a class with java program, it'll search for this function to start execution; 那么这个方法签名就是入口签名,每当你尝试用java程序调用一个类时,它都会搜索这个函数来开始执行; if it doesn;t find it; 如果它没有找到它; it'll just give out an error. 它只会发出一个错误。

Who will pass the vales to String[] array? 谁将vales传递给String []数组? java someprogram value1 value2 value3 java someprogram value1 value2 value3

will automatically populate the array with respective three values. 将自动使用相应的三个值填充数组。 So basically the values are populated when it is run from command prompt and values are passed as parameters against the call. 因此,从命令提示符运行时,基本上会填充值,并将值作为参数传递给调用。

Hope that clears it up 希望能搞清楚

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

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