• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

doubt regarding Boolean constructor  RSS feed

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

This is my first post at JavaRanch. So please pardon me if i miss any rules to post here.

My doubt is regarding Boolean constructor.

This class is having two constructor
public Boolean(boolean value)
pubilc Boolean(String str)

then why
Boolean b = new Boolean(null)
is getting compiled. ???
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you can assign null to String.

String s = null
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thanks for such a quick reply

I am still confused.
If we write
-----------------
String str = null
Boolean b = new Boolean(str)
-----------------
then it should compile.

But why

Boolean b = new Boolean(null)

Is it converting null to string ("null") first ???
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No its not converting to string. The Boolean(String s) constructor takes a string and your string can have null value. If your string has a null value then the Boolean b = new Boolean(null) will result in b being false. If your string does not have null value and is equal, ignoring case, to the string "true" then b will be true.
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!