• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Array question from certpal

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gurpeet. I got it now.
 
Our first order of business must be this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic