5

I got confused with how to use args.length, I have coded something here:

public static void main(String[] args) {
    int[] a = new int[args.length];

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

The printout is 0 no matter what value I put in command line arguments, I think I probably confused args.length with the args[0], could someone explain? Thank you.

Community
  • 1
  • 1
Wang Pei The Dancer
  • 171
  • 2
  • 5
  • 12

6 Answers6

20

int array is initialized to zero (all members will be zero) by default, see 4.12.5. Initial Values of Variables:

Each class variable, instance variable, or array component is initialized with a default value when it is created ...

For type int, the default value is zero.

You're printing the value of the array, hence you're getting 0.

Did you try to do this?

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

args contains the command line arguments that are passed to the program.
args.length is the length of the arguments. For example if you run:

java myJavaProgram first second

args.length will be 2 and it'll contain ["first", "second"].

And the length of the array args is automatically set to 2 (number of your parameters), so there is no need to set the length of args.

Maroun
  • 94,125
  • 30
  • 188
  • 241
2

I think you're missing a code that converts Strings to ints:

public static void main(String[] args) {
    int [] a = new int[args.length];

    // Parse int[] from String[]
    for (int i = 0; i < args.length; i++){
        a[i] = Interger.parseInt(args[i]);
    }

    for (int i = 0; i < args.length; i++){
        System.out.print(a[i]);
    }
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

args[0] is the first element of the args array. args.length is the length of the array

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
1

The a array you iterate on is not the args that contains the actual arguments. You should try:

public static void main(String[] args) {
    for (int i = 0; i < args.length; i++) {
        System.out.print(args[i]);
    }
}
Community
  • 1
  • 1
Guillaume
  • 5,535
  • 1
  • 24
  • 30
1
args.length == 0;

if you're looking for this output:

args[0]:zero
args[1]:one
args[2]:two
args[3]:three

here is the example:

public static void main(String[] args) {
    // array with the array name "arg"
    String[] arg = {"zero", "one", "two", "three"};

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

you have to give a length to the array.

Community
  • 1
  • 1
  • 2
    I don't think this answers the question but I'm also not sure I understand what you're suggesting, could you clarify? Also, format all the code as code. – blm Nov 15 '15 at 03:14
0

The arguements you are passing are stored in args array and not in your so called array a. By default a properly declared array if not initialized takes the default values of its datatypes. In your case 0.

So you can do:

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

or initialize the array a with the args.

Community
  • 1
  • 1
akhil
  • 85
  • 5