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 = args[0];
}
public class DomTest1 {
public static void main(String[] args) {
String file = "test1.xml";
if (args.length > 0) {
file = args[0];
}
}
}
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
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
args is an array of Command Line arguments
Where
args
is an array andif (args.length > 0)
is the condition cheking that array is empty or not .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
The
main()
method is where the execution of java program starts.The place where all parameters passed to main() method are Stringargs[]
. It is basically a String array. The variable name can be changed to something else other than using onlyargs
, You may use Stringvar[]
or `String datas[] or something else.Now, Coming to the
if
condition checking in your programif (args.length > 0)
. I will explain the fundamental of whyarg.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.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.Those are called command line arguments , which you get as a String array in your program. Here is the Oracle tutorial
Hence the below code :
Checks to see if the length of the
String[] args
is greater than0
, which means it checks if any command line argument was entered or is the array empty. If command line arguments were entered , then assignfile
the first element of that array , or else defaultfile
totest1.xml
. You can run your class as :