Search code examples
javaargs

What does argument args.length mean?


I would like to ask you about help explaining args.length !=2 in program below. In book I read they check whether there are two files but why using ! exclamation point? It doesn´t make sence for me. I have found info about args.length but not about this checking file even on stackowerflow and from this reason I am asking here. Thank you.

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;
        }

Solution

  • Java does not put the java class file name in args[], args[] contains only parameters to pass as main() function arguments, so in your example this condition args.length != 2 checks if exactly two arguments are passed to the main() function while executing the commands:

    • javac classFile.java => To compile the java file.
    • java classFile one two => Execute the main() function using 'one' and 'two' as arguments.