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

names
null
Compilation fails
An exception is thrown at runtime
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:
10 comments Page 1 of 1.

Jatinder Verma said:   8 years ago
The output will be b.

Saurabh said:   8 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.

Jack said:   9 years ago
What is args.length? What is there in args?

Rohini Raj said:   9 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.

Nancy said:   10 years 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.

Ritesh said:   10 years 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.

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.

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..

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.

Post your comments here:

Your comments will be displayed after verification.