Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Result of the Code

 
RAMA KRISHNA AALLA
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is result of the below code for
command line-invocation java A 1 2 3

public class A
{
public static void main(String [] args)
{
String [][] array = new String[2][2];
int x;
array[0]=args;
x=array.length;
for(int y=0;y<x;y++)
{
System.out.println(" "+array[0][y]);
}
}
}

A. 0 0
B. 1 2
C. 0 0 0
D. 1 2 3
E. Compilation fails
F. An Exception is thrown at runtime
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you compiled and run the code yourself? If so, then what do you not understand?

Oh, yes, one other thing: please tell us where you got this question from.
Thanks
[ November 20, 2006: Message edited by: Barry Gaunt ]
 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At run time you will get the following exception
ArrayIndexOutOfBoundException
 
RAMA KRISHNA AALLA
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got 1 2 as output but expected Runtime exception.

The question was from K & B.

Thanks,

Rama Krishna
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

What does the statement x = array.length return??? It returns 2 and that is why the o/p 1 2. Is it because array is a two dimensional array, array.length returns 2??

Can anyone explain on this??
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, the array.length returns the no of rows. So that is why the O/P 1 2. I got it. It was worth discussing this topic here.
 
RAMA KRISHNA AALLA
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when the below statement executes

array[0]=args; (Command line Arguments : A 1 2 3)


Thanks,

Rama Krishna
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gives 1 2 as the O/P
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

tricky !

I added only one line to the output and invoked class A from another class:


The last line (I don't mean the "}" ) prints
[[1, 2, 3], [null, null]]

Now what's the length of the array?
What's the length of array[0]?
What's the length of array[1]?


Yours,
Bu.
 
Abdul Rehman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by RAMA KRISHNA CHOWDARY:
I got 1 2 as output but expected Runtime exception.

That's a common misconception in the mind of those who have worked in C++ or similar languages. In Java, there is no such thing as a two dimensional array; rather, nested arrays are arrays of arrays.
In your example, array contains two elements. These elements are actually references to arrays of String i.e. String[]. We can modify the reference contained in the first element of array, to point to any array of String, no matter what its size is.
Hope this will help you understand the output.

Thanks,
Abdul Rehman.
 
RAMA KRISHNA AALLA
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rehman for your explanation.

Regards,

Rama Krishna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic