| Author |
How does this output comes?
|
Sumit Patil
Ranch Hand
Joined: May 25, 2009
Posts: 296
|
|
Hi,
Please study the code below.
The Output is "Object[] array", i am not able to get this behavior.
Can you please help?
Thanks.
|
Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
|
 |
stanislav bashkirtsev
Ranch Hand
Joined: Aug 17, 2009
Posts: 75
|
|
You should pass array of objects! F.i.
|
 |
Sumit Patil
Ranch Hand
Joined: May 25, 2009
Posts: 296
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
|
Please use a meaningful subject line instead of something like "Quite Confusing".
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3670
|
|
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?
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Sumit Patil
Ranch Hand
Joined: May 25, 2009
Posts: 296
|
|
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
Joined: Mar 24, 2008
Posts: 3670
|
|
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
Joined: May 25, 2009
Posts: 296
|
|
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
Joined: Mar 24, 2008
Posts: 3670
|
|
|
Otherwise it would be ambiguous to the compiler because both can accept null.
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
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).
|
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
|
 |
Sumit Patil
Ranch Hand
Joined: May 25, 2009
Posts: 296
|
|
Thanks ....
Got it...
|
 |
 |
|
|
subject: How does this output comes?
|
|
|