Two Laptop Bag*
The moose likes Beginning Java and the fly likes Doubt regarding 2D String arrays in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regarding 2D String arrays in java" Watch "Doubt regarding 2D String arrays in java" New topic
Author

Doubt regarding 2D String arrays in java

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

While trying out some mock tests, I cam across the following question:



My question is that how can we say ? When we are declaring and constructing a 2D array, aren't we supposed to put 2 square brackets on both sides?


~ Mansukh
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

It's confusing because you're thinking in terms of "2D arrays". Java doesn't have multidimensional arrays. String[][] means array of array of String.

If you assign a String[][] to an Object[], it means that each element of the object array will hold a String[].
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Thank you Stephen. Yes , you are correct, There are no 2D/3D/ND arrays in java, but only array or array.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Stephan van Hulst wrote:If you assign a String[][] to an Object[], it means that each element of the object array will hold a String[]...

@Mansukhdeep: It's probably worth adding that you should almost never do this in practise.

In fact, the SCJP exam books are full of code that you would never use in real life; and my advice is to forget it as soon as you leave the exam room - you're unlikely to ever need them again, and they'll probably be more of a distraction than a help.

My advice: Write dumb code.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

OK Winston. Perhaps it is as important to know what not to do as it is to know what is correct.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Mansukhdeep Thind wrote: . . . There are no 2D/3D/ND arrays in java, but only array or array.
Agree. But shouldn’t it read “array of array”?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Aight!! Like Ali G says.. RESPECT main man!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Doubt regarding 2D String arrays in java