Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

null a valid subtype of object ?

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

or


for instance :


null is not a subtype of object.It is a value that is nothing.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic