Java Programming - Language Fundamentals - Discussion

Discussion :: 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

[A]. args[2] = 2
[B]. args[2] = 3
[C]. args[2] = null
[D]. An exception is thrown at runtime.

Answer: Option D

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.


Praveenkumar said: (Jun 4, 2010)  
Here in the code array size is not given. Then how it will be an exception of ArrayIndexOutOfBoundsException.

Ritesh said: (Jun 17, 2010)  
So is it by default that a java program can have at the max 4 arguments?

Ananthraj said: (Jul 6, 2010)  
I am very happy about I am a user of IndiaBIX. I need more explanation.

Sundar said: (Sep 25, 2010)  
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!

Mahesh said: (Jan 29, 2011)  
Thanks Sundar.

Prathyusha said: (Feb 11, 2011)  
Good explanation sundar!..

Anbu said: (May 24, 2011)  
Good explanation mate !

Kiran Gudivada said: (Aug 19, 2011)  
Very nice explanation and I am searching for these type of questions so if you can possible post more. Thank you.

Srieeta Guptaj said: (Oct 28, 2011)  
Very nice said. The answer is very clear to me. Thank you.

Jyothi said: (Feb 4, 2012)  
But
args[0]=CommandArgs
i.e the program name know??

Baldev said: (Mar 4, 2012)  
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: (Jun 27, 2012)  
@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: (Aug 29, 2012)  
@nikhila : Thanks for ur explanation abt Baldevs query .... even me too had the same doubt!

Vijaya said: (Mar 6, 2013)  
@Nikhila.

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

Poornima said: (Apr 3, 2013)  
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: (Jun 10, 2013)  
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: (Jun 21, 2013)  
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: (Dec 8, 2013)  
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.

Balaji said: (Jul 13, 2015)  
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: (Jul 6, 2016)  
@Balaji and @Vivek.

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

Ratnesh Kumar said: (Sep 7, 2016)  
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?

Vinay said: (Mar 20, 2017)  
What should be the correct program with correct command line arguments? Anyone please tell me.

I tried changes in program but it dosent work.

Logaa said: (Jun 24, 2017)  
@Hardik Chavda.

Why it is displaying output only when java CommandArgs 1 2 3 4 is given?

Abiramibabu said: (Nov 13, 2017)  
I cannot understand how it process and get output as, An exception is thrown at runtime.

Nik said: (Apr 18, 2018)  
Thanks @Sundar.

Arora said: (Aug 9, 2018)  
@Sundar,

argv[0] takes the class name, not the input variable. Please explain that how argv[0]=1?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.