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.
Ramesh said:
3 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.
Jatinder Verma said:
9 years ago
The output will be b.
Saurabh said:
9 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.
Jack said:
10 years ago
What is args.length? What is there in args?
Rohini Raj said:
10 years 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.
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.
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.
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.
Arjun said:
1 decade ago
Even X is class but it has been declared in command line so how could it be not recognised.
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..
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers