Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

why this does not give compile-time error ?

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please consider the following question from the certpal mock examination SCJP 1.6 , Question no. 42. why the line Object[] object = new String[5][5]; does not give error.

package certpal42;

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object is a super class of an Array. so you can do this => Object obj = new String[0];Object obj = new String[0][0];and Object obj = new String[0][0][0]; and so on....

now say Object[] it can holds object means new String[0][0] and so on...
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seetharaman. i'm having bit trouble in visualizing 2-d arrays. consider following scenario

class Animal {}

class Dog extends Animal {}

when we write Animal[] a = new Dog[4]; i know that we are creating a tray of references of type A represented as a[0],a[1] and so on which will point to objects of type Dog. first part of my question is i'am right here ? secondly if we have Animal[][] a = new Dog[4][5] , how do the picture look like. let me try what i think . when we write Animal[][] a = new Dog[4][5], is it like we have a tray of references which are pointing to arrays of Animal i.e pointing to array of references of the type Animal which further are pointing to dog objects. i.e. a[0] will point to animal array, which will be a tray of animal references pointing to dog objects. am i thinking right ? is there more simplistic way to visualize this ?

further what happens when we do Object[] obj = new String[4][5] ?
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:Thanks Seetharaman. i'm having bit trouble in visualizing 2-d arrays. consider following scenario

class Animal {}

class Dog extends Animal {}

when we write Animal[] a = new Dog[4]; i know that we are creating a tray of references of type A represented as a[0],a[1] and so on which will point to objects of type Dog. first part of my question is i'am right here ?



That's right, but a small change. There is a type called "Animal Array" which comes before tray of references of type A. So the root is a single reference variable of "array type" which here is represented by "a".(And this "array type" is sub class of Object)
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, let me explain bit more.

Class Animal

class Cat extends Animal

and Cat is an Animal, Cat[] is an Animal[], Cat[] is an Object but Cat[] is not an Animal so an Animal object cant hold Cat[] unlike Object . now clear up to this?

now say Animal[] ani = new Cat[3] [5]; now Animal[] contains 3 Animal reference and each reference pointing to Cat[] length of 5 which is not possible as i said earlier hence error.

but if it could be an Object[] ani = new Cat[3] [5]; now each Object reference can hold cat[5] since cat[] is an object.

does that make sense?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
First of all on the right hand side, you have given array of arrays of string type. Then on the left hand side you have given array of Object type. Every array is an Object, then the array of string is stored in a single Object. It is taking every string array as a Object and it is storing in Object array. That's why it is not giving any compile time error.

Feel free to reply for further clarifications.

Thanks.
 
lowercase baba
Posts: 13013
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:Thanks Seetharaman. i'm having bit trouble in visualizing 2-d arrays.


That shouldn't be a problem, because Java does not HAVE 2-d arrays (or really, any multi-dimensional array). Java only has 1-d arrays.

What you need to wrap your head around is that a java array can hold anything - including an array! So when you say something like "String[][]", what you really get is an array that can hold a bunch of arrays that hold Strings. I have an egg analogy:

an object is an egg
a 1-d array is an egg carton - it holds eggs
a 2-d array is a crate that holds egg cartons which hold eggs
a 3-d array is a shipping pallet that holds crates that hold cartons that hold eggs
etc...

 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot everyone. Got the convoluted but interesting so called 2-d array
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic