This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array question from certpal 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 "Array question from certpal" Watch "Array question from certpal" New topic
Author

Array question from certpal

Surender Suri
Ranch Hand

Joined: May 28, 2010
Posts: 42

what does this array declaration mean ? a string array of 5 with each size of size 5 elements ?



Thanks,
Suri
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Object [] object = new String[5][5];

object is a reference variable of type Object array. now this object reference variable refers to a CONTAINER with 5 elements of type Object. each of the 5 references of the type Object points to a String array with five elements. this is possible because a reference of type Object can refer to any object, hence it is able to refer to String array. even if you do Object object = new String[5][5] it would be compile.
Surender Suri
Ranch Hand

Joined: May 28, 2010
Posts: 42
Thanks gurpeet. One more Q.

how is the answer 1 here ? it should be "devil boast devil boast" right ? or i am missing something ..



1.) devil boast devil devil boast - Answer

2.) devil boast devil boast devil boast devil boast

3.) devil boast devil devil boast devil boast

4.) Runtime error

5.) None of the above
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

correct answer is A only. you are missing a very small part. just a hint and try for yourself. what happens when you create new Boast(eye) object. ? what is the sequence ?

post if you got the answer.

Surender Suri
Ranch Hand

Joined: May 28, 2010
Posts: 42
got it .. Boast(eye) calls the default no-arg constructor of Devil class .. i was thinking it will call Devil(eye) .. but that would require a explicit call to super(eye) ... humm.. what was i thinking :-) thanks gurpeet

this is what happens when you drink beer and take test :-)
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

welcome. they show opposite in movies. in the social network they showed zuckerberg drinking beer and hacking 20+ sites during night. wonder if he really did in real
Karthikeyan Kandasamy
Greenhorn

Joined: Apr 17, 2011
Posts: 14
Hi,

I still could not understand the 1st question / answer.



If i try the following (assignment to String array reference), am getting a compiler error. Why is the special consideration for object array reference variable?.



Compiler Error:
------------------
arrays\ArrayDeclarations.java:10: incompatible types
found : java.lang.String[][]
required: java.lang.String[]
String [] str = new String[5][5];
^
1 error
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

the special consideration is because of the fact that a reference of the type Object can refer to anything.

forget any of above . consider this

Object obj = new String[5][5];

above will compile because of the simple reason that obj is of the type Object which can ofcourse refer to objects of the type Object. since arrays are one of them(i.e. objects) it can refer to them.

when you write Object [] obj = new String[5][5]; this is just an extension of above thing. here you have an array of references of the type Object each of which are able to refer to string array

String [] str = new String[5][5]; here you have a str which is of the type String array . it points to a container which contains references of the type String which must refer to string array which of course is not possible.
Karthikeyan Kandasamy
Greenhorn

Joined: Apr 17, 2011
Posts: 14
Thanks Gurpeet. I got it now.
 
GeeCON Prague 2014
 
subject: Array question from certpal