What Exacly (args.length>0) means?

前端 未结 6 1294
既然无缘
既然无缘 2021-01-27 23:06

This may be simple to you people but as i am new to java, so i want know actually what is going on in the following part?

if (args.length > 0) {
    file =          


        
相关标签:
6条回答
  • 2021-01-27 23:29

    The main() method is where the execution of java program starts.The place where all parameters passed to main() method are String args[]. It is basically a String array. The variable name can be changed to something else other than using only args, You may use String var[] or `String datas[] or something else.

    Now, Coming to the if condition checking in your program if (args.length > 0). I will explain the fundamental of why arg.length is so.

    When a java program is executed from command line or similar terminal, it is run as java customName. Let's say the parameters you want to pass to java program as java customName param1 param2. The arguments are passed along with command line.Now the interpreter in java interprets these paramenters(i.e param1 param2) and passes them to main() method of the program. These parameters are stored in the args[] String Array.

    Now while running java program args[0] and args[1] will be allowed.If no arguments are passed then the value of args[] will be null and will be still be identified as String array object with null parameters(no elements). In that case the args.length will be equal to 0.

    0 讨论(0)
  • 2021-01-27 23:35

    The args.length value is the number of items in the args array.

    If you pass no command-line arguments, you will always get "There are 0 command line arguments".

    thats why you checking

    if (args.length > 0) 
    

    But try running the program like this: java PrintArgs hello my name is mikki2 The words after java PrintArgs are called command line arguments because they are arguments passed to your program from the command line

    0 讨论(0)
  • 2021-01-27 23:42

    Those are called command line arguments , which you get as a String array in your program. Here is the Oracle tutorial

    A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched.

    The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run.

    Hence the below code :

    String file = "test1.xml";
    if (args.length > 0) {
       file = args[0];
    }
    

    Checks to see if the length of the String[] args is greater than 0 , which means it checks if any command line argument was entered or is the array empty. If command line arguments were entered , then assign file the first element of that array , or else default file to test1.xml. You can run your class as :

    java DomTest1  someFileName.someExtension
    

    When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings. In the previous example, the command-line arguments passed to the DomTest1 application in an array that contains a single String: "someFileName.someExtension".

    0 讨论(0)
  • 2021-01-27 23:50

    args is an array of Command Line arguments

    When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings

    Where args is an array and if (args.length > 0) is the condition cheking that array is empty or not .

    0 讨论(0)
  • 2021-01-27 23:51

    You are making String reference here and put the value in it. You first value is> test1.xml. It is a name of a file but you are putting into String as String(It means "test1.xml"). and then taking value from command line argument. And overriding value of you string reference by command line location 0. so you reference value will be always command line 0 location value if you do not pass any value then it will give you text1.xml

    0 讨论(0)
  • 2021-01-27 23:51

    That line is checking to see if arguments were actually entered on the command line.

    If any are entered, the first one is the name of the file.

    If none are entered, test1.xml is the default.

    0 讨论(0)
提交回复
热议问题