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 3 of 3.
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)
Vinay said:
9 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.
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?
Abiramibabu said:
8 years ago
I cannot understand how it process and get output as, An exception is thrown at runtime.
(3)
NIK said:
7 years ago
Thanks @Sundar.
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers