Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - General Questions (Q.No. 4)
4.
public class Myfile 
{ 
    public static void main (String[] args) 
    {
        String biz = args[1]; 
        String baz = args[2]; 
        String rip = args[3]; 
        System.out.println("Arg is " + rip); 
    } 
}
Select how you would start the program to cause it to print: Arg is 2
java Myfile 222
java Myfile 1 2 2 3 4
java Myfile 1 3 2 2
java Myfile 0 1 2 3
Answer: Option
Explanation:

Arguments start at array element 0 so the fourth arguement must be 2 to produce the correct output.

Discussion:
33 comments Page 3 of 4.

Sunil said:   9 years ago
Completely not getting output. Please someone explain it in detail.

Abhishek said:   9 years ago
Why D is not correct?

It also gives output as Arg is 2.

Dave said:   9 years ago
It was really true when we see that program executes that the fourth location must be 2.

Rakesh kumar said:   9 years ago
Agree @Suneel.

This will generate ArrayIndexOutOfBoundException.

Palak said:   8 years ago
Thanks, @Ted.

Your explanation is very helpful to understand.

Jay said:   7 years ago
$javac Example.java
$java -Xmx128M -Xms16M Example
-2 -2.0
1 1.0
same results 2 time(s).

Here is a result for above question.
(1)

Vijaya said:   7 years ago
I Am not understanding please explain correctly.

Harish said:   7 years ago
'java Myfile' is written to run the program after compilation and it's not an argument. Actually arguments start from 1.
args[0]=1
args[1]=3
args[2]=2
args[3]=2

So, option C is the correct answer.

Anil said:   6 years ago
I can't understand this. Please, anyone, help me to get this.

Osama said:   5 years ago
@All.

Here args take the value from the command line i.e command line arguments from the keyboard.

When you compile the program and runs it using java Test .
Along with java Test you have to give the value for an array also like a[0],a[1],a[2] for a[3] give the int value 2 i.e you're passing the value from keyboard to that method here args is a parameter that expects some value from the user so, java Test 1,3,2,2 will be sent from the user keyboard and store it in an array in args so the value of

a[0]=1
a[1]=3
a[2]=2
a[3]=2 remember that a[0] is already been specified.


Post your comments here:

Your comments will be displayed after verification.