File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why this does not give compile-time error ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why this does not give compile-time error ?" Watch "why this does not give compile-time error ?" New topic
Author

why this does not give compile-time error ?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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;

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 04, 2012
Posts: 924
    
    1

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] ?
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
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

Joined: Jan 28, 2008
Posts: 5575

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?
raju salla
Greenhorn

Joined: Jan 05, 2012
Posts: 18
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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11308
    
  16

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...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Thanks alot everyone. Got the convoluted but interesting so called 2-d array
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why this does not give compile-time error ?