What Exacly (args.length>0) means?

前端 未结 6 1293
既然无缘
既然无缘 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.

提交回复
热议问题