参数 args.length 是什么意思?

What does argument args.length mean?

我想请你帮忙解释下面程序中的 args.length !=2。在我读到的书中,他们检查是否有两个文件,但为什么要使用!感叹号?这对我来说没有意义。我已经找到关于 args.length 的信息,但即使在 stackowerflow 上也找不到关于这个检查文件的信息,因此我在这里问。谢谢。

java CopyFile first.txt second.txt - this is put on comand line

import java.io.*;

class CopyFile{
  public static void main(String args[]) throws IOException{

    int i;

      FileInputStream fileIn = null;
      FileOutputStream fileOut = null;

        **//firstly check whether both two files has been set up**
        if(args.length != 2) {
          System.out.println("Using: CopyFile system");
          return;
        }

Java不把javaclass文件名放在args[]中,args[]只包含作为main()函数传递的参数参数,因此在您的示例中,此条件 args.length != 2 检查是否恰好有两个参数在执行命令时传递给 main() 函数:

  • javac classFile.java => 编译 java 文件。
  • java classFile one two => 使用 'one' 和 'two' 作为参数执行 main() 函数。