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

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.

Sundar said:   1 decade ago
Hi Friends,

The array index always starts with '0' that means args[0].

C\:>java CommandArgs 1 2 3 4

The arguments variable 'args' will hold the elements as given below.

args[0] = 1
args[1] = 2
args[2] = 3
args[3] = 4

But in the program we are trying to get the element at 4th index.

String s4 = args[4]; // due to this line an exception is thrown.

I hope that you are all understand this. Have a nice day!

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.

Ratnesh kumar said:   9 years ago
package mypack;

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

Why this one throwing the arrayoutofbound exception?
(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);
}
}

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

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)

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

Vinay said:   8 years ago
What should be the correct program with correct command line arguments? Anyone please tell me.

I tried changes in program but it dosent work.


Post your comments here:

Your comments will be displayed after verification.