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
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.
Arora said:
7 years ago
@Sundar,
argv[0] takes the class name, not the input variable. Please explain that how argv[0]=1?
argv[0] takes the class name, not the input variable. Please explain that how argv[0]=1?
(1)
NIK said:
7 years ago
Thanks @Sundar.
Abiramibabu said:
8 years ago
I cannot understand how it process and get output as, An exception is thrown at runtime.
(3)
Logaa said:
8 years ago
@Hardik Chavda.
Why it is displaying output only when java CommandArgs 1 2 3 4 is given?
Why it is displaying output only when java CommandArgs 1 2 3 4 is given?
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.
I tried changes in program but it dosent work.
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?
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)
Hardik Chavda said:
9 years ago
@Balaji and @Vivek.
You should give arguments as shown below;
java CommandArgs 1 2 3 4
You should give arguments as shown below;
java CommandArgs 1 2 3 4
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);
}
}
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);
}
}
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.
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)
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.
{
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers