• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does this output comes?

 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please study the code below.


The Output is "Object[] array", i am not able to get this behavior.

Can you please help?

Thanks.
 
stanislav bashkirtsev
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should pass array of objects! F.i.
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stanislav bashkirtsev wrote:You should pass array of objects! F.i.


Actually i want to know how the output comes as shown when i pass a 'null'
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use a meaningful subject line instead of something like "Quite Confusing".
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumit Patil wrote:Actually i want to know how the output comes as shown when i pass a 'null'


Because any object reference can be "null". So you are allowed to pass null where a reference of any object is expected.
Compiler matches the most specific one hence the Object[] type argument and not the Object type argument. Is this answer your question?
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Because any object reference can be "null". So you are allowed to pass null where a reference of Object (in this case) is expected


The same holds true for first constructor.
Why does the second constructor gets called and not the first one?
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumit Patil wrote:The same holds true for first constructor.Why does the second constructor gets called and not the first one?


You have asked just when I was editing my reply above which I added the reason why
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You have asked just when I was editing my reply above which I added the reason why


Sorry....

Ok....is this the only reason? why can't it call the first constructor?
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Otherwise it would be ambiguous to the compiler because both can accept null.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually when you pass null to a overloaded method or a overloaded Constructor, the most subtype in the inheritance hierarchy parameter method is chosen and run. As you can see Object[] extends from Object, that is the most subtype in the current scenario(can add a constructor having String[] parameter which will will run). But this is valid only if the parameters fall in the same hierarchy. If some other parameter comes then it will be an error(if String[] is changed to String).
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ....

Got it...

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic