Tuesday, May 9, 2023

What is String args[] Argument in Java Main method? Example

What is String args is used for or Why we use String arg[] in the main method is a common doubt among Java beginners. The first thing newbies are exposed to while learning Java programming language is writing HelloWorldprogram in Java, Though HelloWorld is the smallest sort of program, it contains a lot of useful information and concept for newbies Java programmers, one of them is the main method and its different attributes. Though I covered a 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");
        }
}

What is String args in Java main methodString 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 the 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[]

This 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.


10 comments :

Kannu said...

can you please explain me how this condition is working.

for(String argument : args)

Anonymous said...

@Kannu
it is a "for each" loop, meaning you go through every String in the args array, and each String goes into the String variable "argument" so you can use it!

You can read : "for each String found in args[], put it in 'argument' and do something with it"

Unknown said...

You can even write public static void main(String... args)

Unknown said...

Using Comamd Prompt in Java languagae ,then the compiler will take inputs as a string.

And while Running C language program in Command prompt then why it does not take inputs as Strimgs

Anonymous said...

Why we pass only String object in main method, Can we overload main method to accept other value types as well e.g. Integer, Long or Double? I see no reason, Why we can't overload main method but I am eager to hear from you.

Unknown said...

Yes why java main method Nedd only string array why such things is designed in java? Why there is not array of integer

javin paul said...

@Ronak, I don't have any official source to answer this question, but from my experience and common sense, I think String can represent any value e.g. int, float etc but integer cannot represent text value. Allowing user to array of String gives more flexibility in terms of what he can pass.

Unknown said...

Is string args required in every program
i have been taught in school all the programs without string args

Unknown said...

Why we passing in java main methed in string args pls explain me

Unknown said...

Why we use in java main methed passing string args ex psvm(string args )

Post a Comment