• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Doubt

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class XTC {

public static void main ( String [ ] ka ) {
Integer b = null ;
System . out . println ( b instanceof Object ) ;
}
};
can please explain me why the following snipet is giving me output as FALSE
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

here b is reference of Integer type which is not referring to any object in the heap
that is why you are getting answer as false
try
for this

Integer b=new Integer(10);
now you will get "true"
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With


an object is not created, but only declared.
If you change your code to:

then object of Integer is created and the condition will return true.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi Puluk,

Understand the meaning of "instanceof", this is done at runtime first of all. Reference matters for the compiler only so that it could recognize that left side (of instanceof operator) refernce variable is on the same hierarchy the right hand side class or interface name is.

Your b (Integer reference variable) that refers to null means refers to nothing, alone. So instanceof test always fails in such case. If you make your reference variable b to refer to an Integer object, your instanceof test will result true.



And the rule of thumb, instanceof operator always returns false incase the left side is null.

System.out.println(null instanceof Intgeger); //false
System.out.println(null instanceof StringBuffer); //false

null can be assigned to any reference variable, so no compiler error, but fails instanceof test.

Hope this helps,


Thanks and Regards,
cmbhatt
 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much
 
A teeny tiny vulgar attempt to get you to buy our stuff
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic