Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Main Method in Java
2.1.
Syntax of Method in Java
3.
String args in Java
4.
Why is String args in Java Needed?
5.
Example to Modify our Program to Print Content of String args[] in Java
5.1.
Implementation
5.2.
Compilation
5.3.
Output
6.
Can we execute a java program without main method?
7.
Frequently Asked Questions
7.1.
What is the purpose of String args in Java?
7.2.
Why does main method have String array as an argument?
7.3.
Can we change args in Java?
7.4.
How to pass String args in Java?
7.5.
Why is String immutable in Java?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

What is String Args[] in Java?

Author Riya Singh
2 upvotes
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Have you ever written Java code? What was the first line of your Java main method? Was it “public static void main (String[] args)”? 

As the initial point for starting a Java program, the main method in Java is typically the first method you encounter while learning to code in Java. 

string args in java

In this article, you will learn in detail about What is String Args[] in Java. Before moving to String args in Java, let’s begin with the main method in Java.

Main Method in Java

The main method serves as the starting point for executing a Java program. Any class that is a part of a Java program may contain the main method, which can contain code to run or call other methods. 

Syntax of Method in Java

public static void main (String[] args){
// Some code
}

 

The main method consists of Six terms:

  • Three reserved keywords.
     
  • Name of the main method.
     
  • A Reference Type.
     
  • A variable Name.
Main method in Java
  • public: Here, public represents the access modifier of Java's main method.
    Access modifiers control the accessibility of constructors, methods, and fields of a class.
     
  • static: Java's main method is static. Therefore, using it doesn't need to create any instances beforehand.
     
  • void: The main function of Java is void, which means that upon completion, it returns nothing.
     
  • main: The main method is the function called when the JVM launches a separate program.
     
  • String[]: The main function can accept arguments in the form of an array of configuration parameters.
     
  • args: In Java, the configuration arguments given to the main method are usually referred to as args.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

String args in Java

String args is an array of strings of Java string class, i.e.,  java.lang.String class. The supplied command-line arguments are stored as an array of String objects in String args in java.

Here, the string type is referred to as args(arguments). A string is typically a collection of characters. However, Java strings refer to an object representing a series of characters. In java, a string object is created using the java.lang.String class.

It is worth noting that It's optional to name the array of strings as args. 
Now you might think, can you use anything after the keyword String?

Yes, You can name anything else you like. 

For example, String Coding Ninjas Studio[] or Sting yourname[].

However, most coders prefer to name args because that's what other people do.

Why is String args in Java Needed?

  • Earlier versions of Java(before Java 5) included a method(String args[]) that would take any number of values/arguments needed to construct an array before calling the method. Multiple arguments must still be supplied in an array, but the varargs in the latest java versions feature streamlines and conceals the procedure. 
     
  • A method that expects a variable number of String arguments will be declared with the expression "String... args". In other words, the String args in java can also be written as three periods(...), denoting the possibility of passing an array or a series of arguments in place of only one argument.
     
  • String[] can serve the same purpose just as well, but (String... args) is easier to read and use. Since String[] is a single parameter, (String... args) is an array of parameters of the same type.
     
  • You can combine the java command-line parameters with input received from the console and supplied while the program is executing. String[]args in Java, i.e., a command line argument array, is used in Java to retrieve input from the console.
     
  • It is worth noting that, You can access the String args in Java only in static blocks, such as Main. The rest of your system, including any non-static objects, cannot access it.  

Example to Modify our Program to Print Content of String args[] in Java

Here, you will find an example of String args in Java. The following java code explains the working of String args in Java.

Implementation

public class codingNinjas{
    public static void main(String[] args) {


System.out.println("Example of String args");
System.out.println("Passed String Arguments:");


        for(int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
    }
}

 

Compilation

Use the following command to compile the code mentioned above and to run the resulting codingNinjas file:

java Test Coding Ninjas Studio CodingNinjas Java

 

During the execution of the java command, we passed three arguments, each separated by a space.
Note: You can use double quotes to enclose multiple words if there is some white space between them and you wish to combine them.

Output

Output

Can we execute a java program without main method?

In general, a main method is necessary for executing a Java program.

public static void main(String[] args) {
    // program code goes here
}

 However, there are rare situations in which it may be technically feasible to execute code without a main method, such as using a custom class loader and defining a class with a static block. Despite this possibility, this method is not considered a typical or recommended way of running Java programs. It would not work across all JVM implementations and would not be useful for most use cases. As a result, even though it might be technically possible to run a Java program without a main method in some rare instances, it is not a recommended practice for regular Java programming.

Frequently Asked Questions

What is the purpose of String args in Java?

The string args argument in the main method accepts a single string array. The main method can be declared in both the given following ways. public static void main(String[] args) public static void main(String args[]) Both the above methods will run successfully.

Why does main method have String array as an argument?

The main method in Java has a String array as an argument to receive any command-line arguments passed to the program when it is executed.

Can we change args in Java?

No, It is not possible to modify the actual arguments that are passed to a method in Java, but it is possible to change the values of the method parameters, which are created from those arguments.

How to pass String args in Java?

​​To pass string arguments in Java, one can add them to the command line while running the program. These arguments can be retrieved in the program using the args parameter of the main() method.

Why is String immutable in Java?

Strings are immutable in Java because it ensure their integrity and security. Immutable objects are not prone to unintentional modifications, which makes them suitable for tasks like caching, thread safety, and hashcode-based collections.

Conclusion

In this article, we discussed in detail what String Args[] in Java. We understood how string args[] in java is useful for multiple arguments. The supplied command-line arguments are stored as an array of String objects in String args in java.

We hope this blog has helped you. We recommend you visit our articles on different topics of Java, such as:

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

 

Head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!

Happy Reading!!

Previous article
String valueOf() in Java
Next article
Strings in Java | Part 1
Live masterclass