Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - Finding the output (Q.No. 1)
1.
What will be the output of the program?
public class CommandArgsThree 
{
    public static void main(String [] args) 
    {
        String [][] argCopy = new String[2][2];
        int x;
        argCopy[0] = args;
        x = argCopy[0].length;
        for (int y = 0; y < x; y++) 
        {
            System.out.print(" " + argCopy[0][y]);
        }
    }
}

and the command-line invocation is

> java CommandArgsThree 1 2 3

0 0
1 2
0 0 0
1 2 3
Answer: Option
Explanation:
In argCopy[0] = args;, the reference variable argCopy[0], which was referring to an array with two elements, is reassigned to an array (args) with three elements.
Discussion:
36 comments Page 2 of 4.

Eric said:   1 decade ago
How do I get this program to execute?

Cat said:   1 decade ago
As I don't know anything about java so please explain me clearly. Don't hesitate to help me.

Jyothi said:   1 decade ago
Can anyone explain this clearly because I don't know java.

I'm trying to analyse using C. Help me out.

USHA said:   1 decade ago
Please explain me with simple example.

Nikhil said:   1 decade ago
Please anyone explain code line by line.

NiroshKumar B said:   1 decade ago
args[0]=1.
args[1]=2.
args[2]=3.

In this statement: argcopy[0] = args; -> [[0, 1], [0, 1]] = [1, 2, 3].

0 1 0 //after that.
0 1 2.

-> [[1, 2, 3], [0, 1]] //assign like this.
0 1.

-> So length y is 3 -> [1, 2, 3].
y.

-> argcopy[0][0] = 1.
argcopy[0][1] = 2.
argcopy[0][2] = 3.

Mohammed Salman said:   9 years ago
Hello, Everyone.

1. Initially, argcopy is a reference to a 2D array object.
2. When argcopy[0]=args, that is the "argscopy" reference will be pointing to an array of "args".
3. Hence it prints the value inside the "args" array.
4. Finally, the 2D array becomes an anonymous object.

Hope everyone understands.

Sharma said:   8 years ago
I can't understand. How output will 1, 2, 3?

Priyanka said:   8 years ago
String [][] argCopy = new String[2][2];

argCopy is declared with 2-dimensional array. Their size is 2. How can be assigned the value to a[0][2]?

Rochu said:   1 decade ago
Tajinderpal Singh is wrong because argcopy is an 2D array . it only have argcopy[0][0],argcopy[0][1],argcopy[1][0],argcopy[1][1].
it does not have memory location like argcopy[0][2]=3.


Post your comments here:

Your comments will be displayed after verification.