Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array question from certpal

 
Surender Suri
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gurpeet. I got it now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic