hi, this is a simple program but i have no idea what the args.length is for.

class PrintArgs {

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

i added a few more lines(below) to the code to try to make sense of it but i keep getting "There are 0(zero) command line arguments"

here's the modified version

class PrintArgs 
{

  public static void main (String[] args) 
  {

     for (int i = 0; i < args.length; i++) 
     { 
      System.out.println(args[i]);
    }
    
     System.out.print("There are ");
    System.out.print(args.length); 
    System.out.print(" command line arguments");
    System.out.println();
  }
  
}

Thanks

Dani AI

Generated

Two practical tips that often trip people up with main(String[] args):

  • args is never null. If you do not supply any command-line arguments, the JVM passes an empty array, so args.length is 0. Arrays expose a public final field named length (not a method), which is part of the language, not a user-visible Array class. That is why you write args.length but someString.length(). See the Java Language Specification on array members for details (JLS 10.7).

  • Shells split arguments on whitespace. To pass a single argument that contains spaces, wrap it in quotes, e.g., java PrintArgs "my name" mikki2 so that "my name" is one argument. Program arguments are always strings; parse them as needed.

A minimal pattern that handles missing args, prints them, and safely parses numbers:

public class PrintArgs {
  public static void main(String[] args) {
    if (args.length == 0) {
      System.out.println("Usage: java PrintArgs <arg1> [arg2 ...]");
      return;
    }
    for (String a : args) {
      System.out.println(a);
    }
    try {
      int n = Integer.parseInt(args[0]);
      System.out.println("First arg as int: " + n);
    } catch (NumberFormatException e) {
      System.out.println("First arg is not an int.");
    }
  }
}

In IDEs, set arguments in the run configuration: Eclipse (Run Configurations -> Arguments), IntelliJ IDEA (Run/Debug Configurations -> Program arguments), NetBeans (Project Properties -> Run). Oracle’s tutorial on command-line arguments provides additional context (Command-Line Arguments).

Recommended Answers

All 5 Replies

args.length is the length of the array of commandline arguments.
If you don't pass any arguments that length will be 0 ;)

commented: This was helpful. I couldn't figure out what a commandline argument was. Thanks a ton. +0
Member Avatar for Member #119018

As mentioned before, 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".

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.

Thanks guys!...and as usual...lightning fast answers! (just took a while for me to reply is all)

it is right that args.length is length of cmd line arguments, args is array but length is not a what it is..

An instance variable of Class Array (which is the class behind all arrays). It is an int.

Edit: And, as noted, it represents the length of the array, and since arrays are 0-indexed, the last index of the array is length - 1, of course.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.