The Java program uses the args parameter in main function to realize the transfer of parameters

Source: Internet
Author: User
the Java program uses the args parameter in main function to realize the transfer of parameters

1. While running the Java program, you can use the input parameters to the array of receive parameters in main function args[] for internal application. That is, when you take the arguments behind the Java command line, the Java virtual machine directly stores them in the parameter string array in the Main method.

2..args is a java command-line parameter, because the argument can be multiple, so we use the array to save our Java program in DOS using the Java filename args parameter. Args This array can receive these parameters.

Note: If your program uses command-line arguments, you must specify the appropriate parameters before the Java runtime.

Run the following applet:

Package org.test;
public class Test {public
	static void Main (string[] args) {
		System.out.println (args[0]);
		System.out.println (Args[1]);
		System.out.println (args[2]);
	}

Compiling with console: Javac Test.java
Running: Java Test AAA BBB CCC
Remember to run that line of code followed by three parameters ~ ~ between the parameters separated by a space.

Let me give you an example:

To run the code:

Package org.test;
public class Test {public
	static void Main (string[] args) {
		if (args.length = = 0) {
			System.out.println ("You call MA In method without specifying any arguments. ");
			return;
		}
		SYSTEM.OUT.PRINTLN ("Parameters specified when you call the Main method include:");
		for (int i = 0; i < args.length i++) {
			System.out.println (the value of the parameter + (i + 1) + "is:" + args[i]);
		}
}

You can use this to test results at the command prompt as follows:


If it is called in another program, it can be passed in as a variable parameter or as an array.

3, the above is the command line parameters in the DOS window runtime input method, then in the MyEclipse how to enter command-line parameters.

Method: When MyEclipse runs the Java program, there is an open run Dialog in the run as and then goes there to find a

(x) =arguments then enter the command-line arguments in the first text box in turn.

Program Run Result:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.