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 3 of 3.

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

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

Prathyusha said:   1 decade ago
Good explanation sundar!..

Anbu said:   1 decade ago
Good explanation mate !

NIK said:   7 years ago
Thanks @Sundar.

Mahesh said:   1 decade ago
Thanks Sundar.


Post your comments here:

Your comments will be displayed after verification.