What is String args is used for or Why we use String arg[] in main method
is a common doubt among Java beginners. First thing newbies exposed while
learning Java programming language is writing HelloWorldprogram in Java, Though HelloWorld is
smallest sort of program, it contains lot of useful information and concept for
newbies Java programmers, one of them is main method and it's different
attribute. Though I covered bit of String args[], while discussing
Why
main is public static in Java, I thought to write this article to explain
String args array in more detail. Let's see a simple Java HelloWorld again to
understand more about String args in Java.
public class HelloJava{
public static void main(String args[]){
System.out.println("Helloworld in Java");
}
}
String args[], which is actually an array
of java.lang.String type, and it's name is args here. It's
not necessary to name it args always, you can name it strArray or
whatever you like, but most programmer prefer to name it args, because
that's what everyone else do. When we run
a Java program from command prompt, we can pass some input to our Java
program. Those inputs are stored in this String args array. For example if we
modify our program to print content of String args[], as shown
below:
public class StringArgsTest {
public static void main(String args[]) {
System.out.println("String arguments passed while running this Java Program : ");
for(String argument : args){
System.out.println(argument);
}
}
}
and after compiling, we run this Java program as java StringArgsTest Java J2EE
Android, then this program will print following output :
String arguments passed while running this Java
Program :
Java
J2EE
Android
because, we have passed three arguments separated by white space. If you
want to combine multiple words, which has some white space in between, then you
can enclose them in double quotes. If we run our program again with following
command :
java StringArgsTest "This
is one argument" "This is another argument"
it will print:
String arguments passed while running this Java
Program :
This is one argument
This is another argument
By the way, if you are using Eclipse to run Java Program, then you can
provide String argument to your Java program using "Run
Configuration". When you run Java program, by right click on Java
class with main
method, it creates a Run Argument for that class. you can access them as Right
click-->Run As-->Run Configuration. In second tab, you can see two
section Program Arguments and VM Arguments. You can pass all your String
arguments from this program arguments section. As shown in this image
By the way, you can write your String args[] as String[]
args as well, because both are valid way to declare String
array in Java. Another interesting thing to note is that, from Java 5
onwards, you can varargs
or variable arguments in main method instead of String
args[]. Which means following declaration of main method is also valid : public
static void main(String... args), and you can access this variable argument as
normal array in Java. E.g. if we use this syntax of main, in your earlier
program we don't need to change anything else, it would work, just like it is
working now.
Tidak ada komentar:
Posting Komentar