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.

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.

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.

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

Praveenkumar said:   2 decades ago
Here in the code array size is not given. Then how it will be an exception of ArrayIndexOutOfBoundsException.

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

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.

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

Jyothi said:   1 decade ago
But
args[0]=CommandArgs
i.e the program name know??

Srieeta Guptaj said:   1 decade ago
Very nice said. The answer is very clear to me. Thank you.

Kiran Gudivada said:   1 decade ago
Very nice explanation and I am searching for these type of questions so if you can possible post more. Thank you.


Post your comments here:

Your comments will be displayed after verification.