• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

null instanceof an Object ?

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
null instanceof <anything> always returns false.

jose chiramal wrote:am able to return null here, the compiler doesnt complain. However when i do instanceOf Object it doesnt enter the if loop. How is this possible ?


What do you mean? There is no loop in the code you posted above. (An if-statement does not make a loop!).
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never tried it, but your if condition looks like you are saying if null is an instance of String. Will that ever return true? And can you clarify your question a little? Are you confused as to why it is returning null when you aren't entering your if condition?
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I meant was this :

The method is expecting a return type of Object , and am returning a null value and this compiles successfully. How is this poossible ?

Which violates the below ?

  • null instanceof <anything> always returns false.
  •  
    Sheriff
    Posts: 9707
    43
    Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can assign a null value to any reference variable. But the null value is not an instanceof Object or any other class. When you assign a reference variable null, it means that the reference variable doesn't refer to any object.

    Also this has been asked to you earlier by Christophe and I'm saying it again, try to ask the problem outside of the code. Instead of your question in the code itself, you can give the line that you are confused in a number and ask the question relative to that line number. Eg.


    At line (2) I am able to return null here, the compiler doesnt complain. However when i do instanceOf Object at line (1) it doesnt enter the if loop. How is this possible ?

     
    Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic