File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null a valid subtype of object ?

 
ali kamran
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code tag and source of post please.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*return null* is not only the option then....
 
ali kamran
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

or


for instance :


null is not a subtype of object.It is a value that is nothing.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic