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
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 3 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.
Naresh said:
1 decade ago
Here:
String[][] argCopy = new String[2][2];
Here argCopy[][] will have argCopy[0][0], argCopy[0][1], argCopy[1][0], argCopy[1][1].
When we assign argCopy[0] = args;
Here argCopy[0] will convert into argCopy[0][3] (since arg=3). That's why it will print the those values.
String[][] argCopy = new String[2][2];
Here argCopy[][] will have argCopy[0][0], argCopy[0][1], argCopy[1][0], argCopy[1][1].
When we assign argCopy[0] = args;
Here argCopy[0] will convert into argCopy[0][3] (since arg=3). That's why it will print the those values.
(1)
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.
I'm trying to analyse using C. Help me out.
USHA said:
10 years ago
Please explain me with simple example.
Nikhil said:
10 years ago
Please anyone explain code line by line.
Kiran said:
10 years ago
argCopy[0] = args;
Because of this line argCopy[0] will point to args's base address, suppose 100, so the values are like at 100 ->1, at next address means base+size of the string (length) i.e.101->2, 103->3.
So when we access argCopy[0][0] it will point to base+0 i.e 100 address location that have value 1.
argCopy[0][1] it will point to base+1 i.e 101 address location that have value 2.
argCopy[0][2] it will point to base+2 i.e 102 address location that have value 3.
So it will print 1 2 3, irrespective of declaration[2][2].
Because of this line argCopy[0] will point to args's base address, suppose 100, so the values are like at 100 ->1, at next address means base+size of the string (length) i.e.101->2, 103->3.
So when we access argCopy[0][0] it will point to base+0 i.e 100 address location that have value 1.
argCopy[0][1] it will point to base+1 i.e 101 address location that have value 2.
argCopy[0][2] it will point to base+2 i.e 102 address location that have value 3.
So it will print 1 2 3, irrespective of declaration[2][2].
NiroshKumar B said:
10 years 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.
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:
8 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.
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.
Rajgopal bhallamudi said:
8 years ago
Here is my clear explanation of what is happening internally.
argCopy initially contains [ [null,null],[null,null] ]
argCopy[0]=[null,null]
argCopy[1]=[ [null,null]
while args initially contains 1 2 3.
Now after this assignment argCopy[0] = args;,
argCopy changes to,
[ [1,2,3],[null,null] ]
argCopy initially contains [ [null,null],[null,null] ]
argCopy[0]=[null,null]
argCopy[1]=[ [null,null]
while args initially contains 1 2 3.
Now after this assignment argCopy[0] = args;,
argCopy changes to,
[ [1,2,3],[null,null] ]
(2)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers