The correct syntax for the main method

This is a general question for beginners. I am new to java and browsing through StackOverflow and CodeReview. I find these two different formats:

example 1 :

public static void main(String args[]) 

or
example 2 :

 public static void main(String[] args) 

This is what I have in the course notes:

These words are called modifiers. For the main () method, the word void also precedes that it does not return any value. Also, the main () method always has a list of command line arguments that can be passed to the program

main (String [] args)

which we will now ignore.

So, as you can see, we were told to ignore this for now, but I would like to know:

Is there any difference between them, if so, then what?

+4
source share
5 answers

The Java Language Specification :

[] can be displayed as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable or both.

For instance:

 byte[] rowvector, colvector, matrix[]; 

This declaration is equivalent to:

 byte rowvector[], colvector[], matrix[][]; 
+6
source

The main method takes arguments in a String array, the following paths are accepted

 public static void main(String args[]) public static void main(String []args) public static void main(String... args) 
+3
source

To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array that can reference the variable. Here is the syntax for declaring an array variable:

  dataType[] arrayRefVar; // preferred way. or dataType arrayRefVar[]; // works but not preferred way. 

Note : the type dataType [] arrayRefVar is preferred. The dataType style arrayRefVar [] comes from C / C ++ and was adopted in Java to host C / C ++ programmers.

+3
source

Actaully there is no difference between the two defita def main-method , and both are correct.

But by convention, java prefers declaring the array as String[] args rather than String args[] .

So, this is more conditional -

 public static void main(String[] args){...} 
+2
source

These are all valid core function declarations in Java.

 public static void main(String[] args) { // code } static public void main(String[] args) { // code } static public void main(String args[]) { // code } public static void main(String[] MarkElliotZuckerberg) { // code } public static void main(String... NewYork) { // code } 
  • The keywords public and static are interchangeable, but are required.
  • The main method parameter can accept the var-args syntax.
  • The name can be anything ..!

These are examples of invalid declarations of basic methods -

 static void main(String[] args) { // public is missing } public void main(String args[]) { // static is missing } public static int main(String... Java) { // return type not void return 0; } public void Main(String args[]) { // "main" not "Main" } public void main(string args[]) { // "String" not "string" } public void main(String.. SayHi) { // Ellipses is 3 dots ! } 

I apologize if the source code is not readable ... I always had a problem sending the source code ...: P ... I hope this helps ...! If so, let me know by commenting ..!

Source - Java Tutorials on Programming Theory

0
source

Source: https://habr.com/ru/post/1502737/


All Articles