-3

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;
        }
Domorodec
  • 91
  • 12
  • 1
    `!=` means "not equal to" in Java (and several other languages). I'd recommending reading up on the basic language syntax, see e.g. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html for the various operators. – jonrsharpe Jul 24 '18 at 15:23
  • The logic is looking for exactly two arguments on the command line. If there aren't exactly two arguments, the program prints that little message and returns – nicomp Jul 24 '18 at 15:24
  • Thanks nicomp now it makes me a sence. – Domorodec Jul 24 '18 at 20:06

1 Answers1

1

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.
MiharbKH
  • 206
  • 2
  • 9