File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Why this code does not generate any exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why this code does not generate any exception?" Watch "Why this code does not generate any exception?" New topic
Author

Why this code does not generate any exception?

Lalit Kapoor
Greenhorn

Joined: Jun 06, 2004
Posts: 20
public class Q {
public static void main(String argv[]){

int anar[]=new int[]{1,2,3};

System.out.println(anar[2]);

}

}

The output of this code is 3. But why? As there is no third row in the two dimensional array. What is the reason behind this?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi Lalit,

I don't see any two-dimensional arrays; "new int[]{1,2,3}" creates a one-dimensional array with three numbers in it. Creating a two-dimensional array with the bracket-initialization syntax would look like

int anar[][] = new int[][]{{1,2,3}, {4,5,6}, {7,8,9}};

or, more simply,

int anar[][] = {{1,2,3}, {4,5,6}, {7,8,9}};


[Jess in Action][AskingGoodQuestions]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Java does not support multi-dimensional arrays.
http://www.xdweb.net/~dibblego/java/faq/answers.html#q45


Tony Morris
Java Q&A (FAQ, Trivia)
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
In that link, I am assured that
This distinction [that what might be mistakenly called multidemensional arrays are in fact arrays of arrays] is important in coming to terms with the Java programming language, and is not purely academic as some might suggest.


I am then presented with some pretty generic code using arrays of arrays, and informed that
It is important to realise, that in the above code sample, there is no "2 dimensional" array. There is an array whose elements are String arrays.


However, I am never told why this is important to realize. In what instance would this knowledge influence my use of arrays?
[ June 08, 2004: Message edited by: Joseph George ]

I've heard it takes forever to grow a woman from the ground
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

The difference influences what you can and can do as far as initializing and working with arrays, but mostly "around the edges." The distinction is unimportant for most straightforward uses. As the web page points out, a "2D" array doesn't have to be rectangular, and this influences how nested loops should be written, but only rare code exploits this fact.

C(++) supports both kinds of arrays, but (a) doesn't support Java's shortcut syntax for initializing the array-of-pointers-to-arrays kind, and (b) requires you to deallocate the memory for the sub-arrays manually, so in that language the distinction is much more important.
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
[IDLE CURIOSITY]
how does a non-rectangular "2D" array work?
[\IDLE CURIOSITY]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why this code does not generate any exception?
 
Similar Threads
Dharmesh Rathod (Info)
doubt(marcus mock test)
mock exam question
array
what difference does it make ...arrayini