• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt regarding Boolean constructor  RSS feed

 
Brij Garg
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. ???
 
sridhar row
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!