Java Programming - Language Fundamentals - Discussion
Discussion Forum : Language Fundamentals - Finding the output (Q.No. 8)
8.
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
String names [] = new String[5];
for (int x=0; x < args.length; x++)
names[x] = args[x];
System.out.println(names[2]);
}
}
and the command line invocation is
> java X a b
Answer: Option
Explanation:
The names array is initialized with five null elements. Then elements 0 and 1 are assigned the String values "a" and "b" respectively (the command-line arguments passed to main). Elements of names array 2, 3, and 4 remain unassigned, so they have a value of null.
Discussion:
11 comments Page 1 of 2.
Rohini Raj said:
1 decade ago
Hello every using notepad the above code works.
But "using Eclipse IDE" as per below code output is as follows.
I don't understand how to assign a, b parameters to names[0] and names[1]. Please help me in solving this, using Eclipse IDE.
public class X
{
public static void main(String [] args)
{
String names [] = new String[5];
for (int x=0; x < args.length; x++)
names[x] = args[x];
System.out.println(names[0]);
System.out.println(names[1]);
System.out.println(names[2]);
}
}
Output:
null.
null.
null.
But "using Eclipse IDE" as per below code output is as follows.
I don't understand how to assign a, b parameters to names[0] and names[1]. Please help me in solving this, using Eclipse IDE.
public class X
{
public static void main(String [] args)
{
String names [] = new String[5];
for (int x=0; x < args.length; x++)
names[x] = args[x];
System.out.println(names[0]);
System.out.println(names[1]);
System.out.println(names[2]);
}
}
Output:
null.
null.
null.
Alkis said:
1 decade ago
Taking the name of the program in the arguments is seen in C. Java on the other hand doesn't do that. Check this with any parameters you want.
public class Mpla {
public static void main(String [] args)
{
for (int y = 0; y < args.length; y++)
{
System.out.print(" " + args[y]);
}
}
}
It will not print Mpla.
public class Mpla {
public static void main(String [] args)
{
for (int y = 0; y < args.length; y++)
{
System.out.print(" " + args[y]);
}
}
}
It will not print Mpla.
Saurabh said:
10 years ago
@Jack: public static void main(String [] args).
Here, args is a reference variable of type String array type. It's like void display(int a): here a is "a" is a variable of type "int".
args.length() is a pre defined method which tells about the length of the array which is defined.
Ex: String names[] = new String[5]; here, length of array is 5.
Here, args is a reference variable of type String array type. It's like void display(int a): here a is "a" is a variable of type "int".
args.length() is a pre defined method which tells about the length of the array which is defined.
Ex: String names[] = new String[5]; here, length of array is 5.
Ramesh said:
8 months ago
@All.
Here I got the Output :
C:\Users\user\OneDrive\Desktop>javac X.java
C:\Users\user\OneDrive\Desktop>java X Java X a b
a
Note : Entering Four Elements then args.length =4 and the loop iterates from o to3. names[0]=args[0];// Java
names[1]=args[1]; // X
names[2]=args[2]; // a
names[3]=args[3]; // b
we are printing names[2]; // a is the output.
Here I got the Output :
C:\Users\user\OneDrive\Desktop>javac X.java
C:\Users\user\OneDrive\Desktop>java X Java X a b
a
Note : Entering Four Elements then args.length =4 and the loop iterates from o to3. names[0]=args[0];// Java
names[1]=args[1]; // X
names[2]=args[2]; // a
names[3]=args[3]; // b
we are printing names[2]; // a is the output.
Ritesh said:
1 decade ago
Why an ArrayOutOfBound exception is not thrown in this case?
As args[2], args[3], args[4] does not exist and we are assigning these value to name[x].
Please see the following link. In ques.2, this concept is used.
http://www.indiabix.com/java-programming/language-fundamentals/002001
Please someone solve my confusion.
As args[2], args[3], args[4] does not exist and we are assigning these value to name[x].
Please see the following link. In ques.2, this concept is used.
http://www.indiabix.com/java-programming/language-fundamentals/002001
Please someone solve my confusion.
Nancy said:
1 decade ago
@Ritesh.
In this question we have already declared a string of size 5. Hence the values are a, b, null, null, null.
And in that 2nd question of that link string is not declared and if we assign value that does not exist it gives Array out of bound Exception.
In this question we have already declared a string of size 5. Hence the values are a, b, null, null, null.
And in that 2nd question of that link string is not declared and if we assign value that does not exist it gives Array out of bound Exception.
Vaibhav Gupta said:
1 decade ago
What about the X. I think the value of X should be assigned to element 0, a and b be assigned respectively to 1st and 2nd element of the names array.
Mohit said:
1 decade ago
X is class. hence its java X, a and b are parameters who assigned as 0th n 1st elemnts n others as null..
Arjun said:
1 decade ago
Even X is class but it has been declared in command line so how could it be not recognised.
Jack said:
1 decade ago
What is args.length? What is there in args?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers