Home >Java >javaTutorial >Sample code sharing about main function in Java

Sample code sharing about main function in Java

黄舟
黄舟Original
2017-09-26 09:50:401608browse

This article mainly introduces the relevant information about the detailed introduction of the main function in Java. The main() function must appear in the Java program. Here is the usage method. Friends who need it can refer to it

Detailed introduction to the main function in Java

The main function in JAVA is something we are very familiar with. I believe that everyone who has studied the JAVA language can write it skillfully. The entry function of this program is written, but not everyone can easily answer why the main function is written this way and what each keyword in it means. I also encountered this problem while studying. After searching for information on the Internet and adding my own practice, I finally got some insights. I didn't dare to keep them, so I wrote them down to share with everyone.

The general way of writing the main function is as follows:


 public static void main(String[] args){…}

The functions of these keywords are explained below:

(1) public keyword , this is easy to understand, declaring the main function as public is to tell other classes that they can access this function.

 (2) The static keyword tells the compiler that the main function is a static function. That is to say, the code in the main function is stored in the static storage area, that is, this code already exists after the class is defined. If the main() method does not use the static modifier, there will be no compilation error, but if you try to execute the program, an error will be reported, indicating that the main() method does not exist. Because the class containing main() has not been instantiated (that is, there is no object of this class), its main() method will not exist. Using the static modifier means that the method is static and can be used without instantiation.

 (3) The void keyword indicates that the return value of main() is untyped.

 (4) Parameter String[] args, this is the focus of this article.

First, program users can pass parameters to a class in the command line state. Look at the following example:


public class ArgsDemo {
  public static void main(String[] args) {
  String str = new String();
  for (int i = 0; i < args.length; i++) {
  System.out.println(args[i]);
  str += args[i];
  }
  System.out.println(str);
  }
  }

Use the javac ArgsDemo.java command to generate the ArgsDemo.class file; then use "java ArgsDemo parameter one parameter two parameter three..." The format passes parameters to the ArgsDemo class. This example program will first output the parameters and then output the sum of all parameters. For example, java ArgsDemo a b c will get the following output:


  a

  b

  c

  abc

. It should be noted that if the loop condition here is not i


Exception in thread “main” java.lang.ArrayIndexOutOfBoundException:3

  at ArgsDemo.main(ArgsDemo.java:5)

Second, you can pass parameters to the class containing main() in another class, as in the following example:


public class A {
  public static void main(String[] args)
  {
  for(int i=0;i <args.length;i++)
  System.out.println(args[i]);
  }
  }
  public class B {
  public static void main(String[] args)
  {
  c = new A();
  String[] b = {"111","222","333"};
  c.main(b);
  }
  }

First define a class A, in Define a main() function in A, and output the parameter args in this function. Then define a classB, initialize an instance c of A in B, then pass parameters to c, and call the main method of c to print out the passed parameter values. The output result is as follows:


 111

 222

 333

Since the main() function is a static function, it can be used without instantiation, so B can also complete the same function using the following writing method :


public class B {
  public static void main(String[] args)
  {
  //A c = new A();
  String[] b = {"111","222","333"};
  A.main(b);
  }
  }

Summary: The main function of parameter args is to provide a means for program users to interact with the program in the command line state. In addition, it is also feasible to use the main() function directly in other classes and pass parameters. Although this method is not commonly used, it provides us with an option after all.

The above is the detailed content of Sample code sharing about main function in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn