Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - Finding the output (Q.No. 2)
2.
What will be the output of the program?
public class CommandArgs 
{
    public static void main(String [] args) 
    {
        String s1 = args[1];
        String s2 = args[2];
        String s3 = args[3];
        String s4 = args[4];
        System.out.print(" args[2] = " + s2);
    }
}

and the command-line invocation is

> java CommandArgs 1 2 3 4

args[2] = 2
args[2] = 3
args[2] = null
An exception is thrown at runtime.
Answer: Option
Explanation:

An exception is thrown because in the code String s4 = args[4];, the array index (the fifth element) is out of bounds. The exception thrown is the cleverly named ArrayIndexOutOfBoundsException.

Discussion:
26 comments Page 2 of 3.

Baldev said:   1 decade ago
class CommandArgs
{
public static void main(String [] args)
{
String s1 = args[1];
String s2 = args[2];
String s3 = args[3];
//String s4 = args[4];
System.out.print(" args[2] = " + s2);
}
}


now also runtime excption

Nikhila said:   1 decade ago
@Baldev.

The above program also gives run time exception since the array index starts with 0 so, declare/initialize the string s1=args[0];

class CommandArgs
{
public static void main(String [] args)
{
String s1 = args[0];
String s2 = args[1];
String s3 = args[2];
//String s4 = args[3];
System.out.print(" args[2] = " + s2);
}
}

Compile and run the program while you run the program give the values there itself like java CommandArgs 10 20 30

You will get the output successfully.

Varsha said:   1 decade ago
@nikhila : Thanks for ur explanation abt Baldevs query .... even me too had the same doubt!

Vijaya said:   1 decade ago
@Nikhila.

I tried your code in eclipse but still getting array out of boundary exception.

Poornima said:   1 decade ago
args[1]=0(1)
args[2]=0,1(1,2)
args[3]=0,1,2(1,2,3)
args[4]=0,1,2,3(1,2,3,4)

String s2=args[2]

args[2]=0,1,2

Command Args are 1 2 3 4

Result is "ArrayIndexOutOfBoundsException".

Vivek said:   1 decade ago
class test{
public static void main(String [] args)
{
String s1 = args[0];
String s2 = args[1];
String s3 = args[2];
String s4 = args[3];
System.out.print(" args[2] = " + s2);
}
}

OUTPUT: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 is shown. Any one help me.

Laxmimanohar said:   1 decade ago
public class CommandArgs
{
public static void main(String [] args)
{
String s1 = args[0];
String s2 = args[1];
String s3 = args[2];
String s4 = args[3];
System.out.print(" args[2] = " + s2);
}
}
Save it as CommandArgs.java

Compile it and run the program as follows,
"java CommandArgs 1 2 3 4" and you will get output as args[2] = 2.

Sri said:   1 decade ago
It's take 4 is size becoz we have entered 4 args so, array starts it's memory allocation from the 0th element.

from cmd pmt we entered 1 2 3 4.

args[0]=1;
args[1]=2;
args[2]=3;
args[3]=4;
args[4]=? we are not allocated any value to this one but in the array it will shows empty.
(1)

Balaji said:   1 decade ago
Still not getting correct answers please guys guide me. It show oly out of bound array.

public class CommandArgs
{
public static void main(String [] args)
{
String s1 = args[0];
String s2 = args[1];
String s3 = args[2];
String s4 = args[3];
System.out.print(" args[2] = " + s2);
}
}

Hardik Chavda said:   9 years ago
@Balaji and @Vivek.

You should give arguments as shown below;
java CommandArgs 1 2 3 4


Post your comments here:

Your comments will be displayed after verification.