• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

1Z0-808 Java Guide by Jeanne - Chapter 3 Arrays

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
 
Author
Posts: 285
12
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no such thing as multi-dimensional arrays in Java. We have arrays of anything, including other arrays. But arrays are objects, so, in a seemingly two-dimensional situation, you actually have an array of objects (specifically, arrays). The default value of any object type variable/value is null.

Another way to put this is to point out that mArr2 is not a two-dimensional array of double, it's a single dimension array of pointers/references to arrays.

It's also worth mentioning that Arrays.toString does not behave the same as Arrays.deepToString.

Try these two changes (first just the first one, then add the second too)

1) add a second "dimension" value to your code like this:

Run it and see the output

2) Add another print statement:

Run it and see again.

Help any?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simon Roberts wrote:There's no such thing as multi-dimensional arrays in Java.


Agreed. Oracle used that word in the objectives for the Java 8 exam so we used it too.
 
Ashwini Masuti
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Simon,

Thanks for your reply and explanation,
Change 1) double[][] mArr2 = new double[2][2];
System.out.println("mArr2 - "+Arrays.toString(mArr2));  // Output --> mArr2 - [[D@5ca881b5, [D@24d46ca6]

2) Added Arrays.deepToString(mArr2); // output -  mArr2 with deepToString - [[0.0, 0.0], [0.0, 0.0]]

 
Simon Roberts
Author
Posts: 285
12
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:

Simon Roberts wrote:There's no such thing as multi-dimensional arrays in Java.


Agreed. Oracle used that word in the objectives for the Java 8 exam so we used it too.


Yes, and apologies--I didn't intend that comment to come over as criticism of your choice of terminology! There are many places where "multidimensional array" gets used in Java docs, and heck, in my own writing and teaching. I merely wanted to highlight the implementation detail that I hoped would explain OP's observed behavior
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No worries. I didn't want the OP to think the book he is reading doesn't know what's going on .
 
Simon Roberts
Author
Posts: 285
12
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:No worries. I didn't want the OP to think the book he is reading doesn't know what's going on .



Indeed, I realized when you raised the issue that I might have made OP think badly of you for no good reason! I hope not
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic