Is there a difference between main(String args[])

2019-01-08 15:01发布

Is there a difference between:

public void main(String args[]) { ... } 

and

public void main(String[] args) { ... }

I don't believe so, but I am wondering.

9条回答
\"骚年 ilove
2楼-- · 2019-01-08 15:24

Both of them are absolutely the same. Please refer to Java Language Specification (JLS) to see the syntax used in java.

String[] args or String args[] will create an array (reserves a place in memory)with no size and name args.

Let us Consider:

String [] x;

putting the [] after the name. Why? because it avoids subtle problems like this:-

String x [] , y; //x[] is an array but y is a String
String [] x y ; //two arrays x[] and y[] both
查看更多
对你真心纯属浪费
3楼-- · 2019-01-08 15:28

Nope. There is no difference b/w the two.

See Declaring a Variable to Refer to an Array section in this doc. From that doc

float anArrayOfFloats[]; // this form is discouraged

so just use the second style.

查看更多
女痞
4楼-- · 2019-01-08 15:29

There's no difference, but putting the brackets after the type (String[]) is the more common practice in Java.

查看更多
Rolldiameter
5楼-- · 2019-01-08 15:33

The method signature is the same so there is no difference.

Its a public method, it returns nothing, the method name is "main" and the it takes a String array.

查看更多
【Aperson】
6楼-- · 2019-01-08 15:33

The difference between these is that when we try to call the main method manually from one class to another by using static syntax , we use "string[] s ". At that point we have to pass an array of string type as argument in main method. When we are using "String... s" then there is no need to pass any string array as an argument. It will run and it doesn't matter if you pass the array as an argument or not (if you don't explicitly pass it, it is passed by itself) ... hence proved/////////

查看更多
The star\"
7楼-- · 2019-01-08 15:34

there is a difference: For example if you write int[] arr; - here arr is the reference to the integer array whereas in case of int arr[]; - arr is a primitive int type array

查看更多
登录 后发表回答