Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference Boolean(boolean), Boolean(String) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference Boolean(boolean), Boolean(String)" Watch "Difference Boolean(boolean), Boolean(String)" New topic
Author

Difference Boolean(boolean), Boolean(String)

Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
What is the difference between Boolean(boolean), Boolean(String) constructors. Where do we use the second one?
Thanks


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
It's important to remember that the boolean literal, true, is very different from the String literal, "true". These two lines of code would use the two constructors, respectively:

I hope that helps,
Corey


SCJP Tipline, etc.
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
Corey, I understand the constructors. But where do we use the second one? We use both true and false for boolean. But why do we need string? A tiny exmaple is appreciated.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Any time you have a String that contains a boolean literal, you may want to use this approach. Perhaps you're taking some input from a user and you know that the input will be either "true" or "false". You'll want to turn that input (which is a String) into a boolean, so you'll have to use the String constructor. Here's an example:

Granted, I doubt this would be used vary often. However, if you were to create an expert system, this may be a useful technique. Of course, Java isn't usually the language of choice for such a system, but the example just popped to mind.
I hope that helps,
Corey
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

very nice example
Also, remember that the java.lang.Boolean(String) contructor gives some leeway -- it's case INsensitive.
So Boolean("tRuE") and Boolean("true") both evaluate to Boolean.TRUE
[ June 03, 2002: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
thanks a lot.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Jessica Sant:
it's case INsensitive.
So Boolean("tRuE") and Boolean("true") both evaluate to Boolean.TRUE

Great point, Jess. I overlooked that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference Boolean(boolean), Boolean(String)
 
Similar Threads
JTable And CheckBox
why Boolean return type in equalgnoreCase(String s)?
1) Request Attributes? 2) SessionListener?
String Test
Boolean: valueOf() and equality doubt