| Author |
null a valid subtype of object ?
|
ali kamran
Greenhorn
Joined: Apr 25, 2010
Posts: 29
|
|
Given this code snippet, what can be inserted in the fragment ?
this is from certpal
can somebody please explain why null is valid code fragment
|
 |
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
|
Code tag and source of post please.
|
Tell the difficulties that i am difficult.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
ali kamran wrote:can somebody please explain why null is valid code fragment
null is an valid return type in this regard. by the way: what are the other options?
|
 |
ali kamran
Greenhorn
Joined: Apr 25, 2010
Posts: 29
|
|
Seetharaman Venkatasamy wrote:
ali kamran wrote:can somebody please explain why null is valid code fragment
null is an valid return type in this regard. by the way: what are the other options?
May you please elaborate "in this regards" ?.The correct answers is 2,3 and 4.The return type is of the same class or a sub class.how is this and null subtype ?
Options
1.) return new Object();
2.) return this;
3.) return new Marmaduke();
4.) return null;
5.) return "";
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
*return null* is not only the option then....
|
 |
ali kamran
Greenhorn
Joined: Apr 25, 2010
Posts: 29
|
|
Seetharaman Venkatasamy wrote:*return null* is not only the option then....
May you please explain how return null is valid.Thank you
|
 |
saloni jhanwar
Ranch Hand
Joined: Feb 09, 2012
Posts: 583
|
|
or
for instance :
null is not a subtype of object.It is a value that is nothing.
|
 |
gurpeet singh
Ranch Hand
Joined: Apr 04, 2012
Posts: 868
|
|
Return types go through assignment-conversions(for more information refer JLS http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2) .
basically it says in assignment conversions, the type of expression must be converted to the type of the variable. also if you look in the return type section in JLS , you will see that they use assignment conversion. so whatever you return must be assignable to the return type mentioned in your method using assignment conversion rules/contexts given in the book. since null can be assigned to any object reference variable, null is a valid return type from any method returning any object.
|
OCPJP 6(100 %) OCEWCD 6(91 %)
|
 |
 |
|
|
subject: null a valid subtype of object ?
|
|
|