简体   繁体   中英

why testing args.length?

I'm a Java beginner and I'm confused about testing args.length at the begining of many codes, and why it's never gets higher than 0 in any of my codes?

import java.net.Socket;
import java.net.UnknownHostException;
import java.io.IOException;

public class LowPortScanner {
public static void main(String[] args) {
String host = "localhost";
if (args.length > 0) {
host = args[0];
}
for (int i = 1; i < 1024; i++) {
try {
Socket s = new Socket(host, i);
System.out.println("There is a server on port " + i + " of "
+ host);
}
catch (UnknownHostException ex) {
System.err.println(ex);
break;
}
catch (IOException ex) {}
} // end for
} // end main
} // end PortScanner

You have to inputs to main mathod from Command prompt.

like below

java LowPortScanner TEST1 TEST2

Because if it is not tested then an exception would be thrown because host = args[0]; would illegal.

However, this doesn't look like it's going to help much, an empty or null host looks like it would cause further problems.

If the length of args is always 0 , then be sure you're actually passing in parameter arguments.

If there are no command arguments the args[0] will fail. This is why it must be protected.

It depends on how you invoke the Java class file. In command prompt or bash shell:

java LowPortScanner Argument1

typing the above line in the command prompt/bash will cause the argument count to increase to 1. (because Argument1 is one argument, after the class file LowPortScanner )

java LowPortScanner Argument1 Argument2

the above line will make argument count to increase to 2.

hence args.length will be 2 in the second case and 1 in the first case.

If you are calling your program from CMD or bash you can asign ARGuments it like
java LowPortScanner google.com

Then "google.com" is your args[0]. When your program supports commandline attributes it is recommended to test if the given arguments are corret.

args in public static void main(String[] args is the String array of arguments passed from command line.

java LowPortScanner argument1 argument2

if you try the above command args.length will return 2 .

As far as question of checking the length it is done for java programs which can take command line arguments and if arguments are not passed then they prompt for input.

if(args.length >0 ){
  //proceed using passed arguments
}else{
   //proceed with some default value
}

You are running your program using java LowPortScanner hence no arguments are passed and args.length is always zero.

Moreover if you don't pass any argument and use host=args[0] you will get ArrayIndexOutOfBoundsException .

如果不传递任何参数,则变量String [] args会将所有参数传递给整个程序,如果不传递任何参数,则args的长度将变为0。现在最好在访问它之前检查其长度,否则有机会获取ArrayIndexOutOfBoundsException如果它的大小是0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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