This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes null a valid subtype of object ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "null a valid subtype of object ?" Watch "null a valid subtype of object ?" New topic
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: 924
    
    1

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: null a valid subtype of object ?
 
Similar Threads
constructor up and down hierarchy
Co variant returns
Overriding Fragments
Why Is a No-Arg Constructor Required Here?
Error while compiling