• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

instanceof with boolean

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class InstanceTest {//line1
public static void main(String [] args) {//line2
String a = null;//line3
boolean f=false;//line4
boolean g;//line5
boolean b = null instanceof String;//line6
boolean c = a instanceof String;//line7
boolean d = f instanceof boolean;//line8
System.out.println(b + " " + c+" "+d);//line9
}//line10
}//line11


am getting error in line number 8.
cant we compare boolean variables using instancesof operator?

kindly help me out

regards,
muskaan
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, because boolean is a primitive data type. You can only do the comparison if it's a class or interface.
 
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
"muskaan", please check your private messages. You can see them by clicking My Profile.
 
muskaan gaffor
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
boolean d = f instanceof Boolean

even this gives error....

kindly explain me how to compare the boolean variables??

regards,

Muskaan
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by muskaan bg:
boolean d = f instanceof Boolean

even this gives error....



That's because "f" is not an object. instanceof only works with objects, not primitives.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !!


I have a doubt on the above Question please
if anyone could clarify


String a = null;//line3

boolean b = null instanceof String;//line6
boolean c = a instanceof String;//line7


why
null instanceof String gives False?
a instanceof String gives False?
although a is a string


Please if anyone can explain!!!
it will be kinda of you..........!!!
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why
null instanceof String gives False?
a instanceof String gives False?
although a is a string


1) null instanceof String gives False?: null does not pass the IS-A test for String. so null is not a String. And hence we get a false. IS-A test is passed when the left hand operator is a class that extends the class on the right hand side, or the left hand side implements the interface on the right hand side.

a instanceof String gives False?....this fails because of the same reason as above. Instance of does not compare the reference variables. It compares the actual objects at runtime. null means no object..."No Object".

Hope this helps.

Thanks,
Raghu.
 
Jesper de Jong
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
muskaan bg - Please check your private messages again for an important message from JavaRanch.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Raghu

Thanks for the explanation now i got my
doubt cleared!!!
reply
    Bookmark Topic Watch Topic
  • New Topic