Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

convert boolean to String jdk1.3

Thread view: 
Petterson Mikael - 15 Mar 2006 13:57 GMT
Hi,

How can I convert a primitive boolean to String?

cheers,

//mikael
Gordon Beaton - 15 Mar 2006 14:14 GMT
> How can I convert a primitive boolean to String?

There's this static method:

 String s = Boolean.toString(bool);

Or you could have thought up this complex piece of code:

 String s = bool ? "true" : "false";

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Lars - 15 Mar 2006 17:39 GMT
>>How can I convert a primitive boolean to String?
>
> There's this static method:
>
>   String s = Boolean.toString(bool);

quicker and dirtier:

  s = "" + bool;
Roedy Green - 16 Mar 2006 18:43 GMT
>quicker and dirtier:
>
>   s = "" + bool;

terser and dirtier. There are quicker ways.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Mishagam - 15 Mar 2006 17:53 GMT
>>How can I convert a primitive boolean to String?
>
[quoted text clipped - 7 lines]
>
> /gordon

For me standard Java way (for integers and so on):

boolean bool;
s = "" + bool;

worked OK.
Thomas Fritsch - 15 Mar 2006 18:00 GMT
> For me standard Java way (for integers and so on):
>
> boolean bool;
> s = "" + bool;
>
> worked OK.

To avoid the dummy string-concatenation you should use
  s = String.valueOf(bool);
There are other String.valueOf(...) methods for all primitive types.

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Steve W. Jackson - 15 Mar 2006 19:39 GMT
> > For me standard Java way (for integers and so on):
> >
[quoted text clipped - 6 lines]
>    s = String.valueOf(bool);
> There are other String.valueOf(...) methods for all primitive types.

It happens that this method uses exactly the same code in Sun's 1.4.2 as
Boolean.toString(bool) to do that task.  But I've discovered that it's
sometimes better to use the static toString method from the appropriate
class instead of String.valueOf.  String.valueOf(int), for instance,
passes on the call to Integer.toString(int, 10) and thus assumes
decimal.  Likewise for long.

In general, though, one or the other of these is far better than simple
string concatenation.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Mishagam - 16 Mar 2006 02:07 GMT
>> For me standard Java way (for integers and so on):
>>
[quoted text clipped - 6 lines]
>   s = String.valueOf(bool);
> There are other String.valueOf(...) methods for all primitive types.

For me , "" + bool is shorter, clearer and much easier to remember. I
don't understand why I should avoid it. It is one of things that make
Java better than C/C++. I didn't remember about existence of valueOf()
method, and doubt that I will ever use it.
Alan Krueger - 16 Mar 2006 07:22 GMT
> For me , "" + bool is shorter, clearer and much easier to remember. I
> don't understand why I should avoid it.

Because that compiles into this:

StringBuffer temp = new StringBuffer();
temp.append(bool);
s = temp.toString();

Boolean.toString() simply returns a static string, no construction
necessary.
Hendrik Maryns - 16 Mar 2006 10:53 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Mishagam schreef:

>>> For me standard Java way (for integers and so on):
>>>
[quoted text clipped - 11 lines]
> Java better than C/C++. I didn't remember about existence of valueOf()
> method, and doubt that I will ever use it.

Except from Alan's comment, I strongly disagree that this is clean and
legible code.  It is a trick.  You have to know details of Java syntax
to know it, whereas String.valueOf(boolean) or Boolean.toString(boolean)
comply to the standard static method syntax.

H.
Signature

Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org

Steve W. Jackson - 15 Mar 2006 19:35 GMT
> > How can I convert a primitive boolean to String?
>
[quoted text clipped - 7 lines]
>
> /gordon

Out of curiosity, why would you consider that complex?  FWIW, it's
exactly the code I see in the String.valueOf(boolean) and
Boolean.toString methods in Sun's code (1.4.2).

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Gordon Beaton - 15 Mar 2006 19:48 GMT
> Out of curiosity, why would you consider that complex?

I don't, it was an attempt at sarcasm. The OP has been posting here
for something like 5 or 6 years, and I would expect that he should be
capable of solving that kind of problem by now.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Roedy Green - 15 Mar 2006 20:11 GMT
On Wed, 15 Mar 2006 13:57:58 +0100, Petterson Mikael
<mikael.petterson@era.ericsson.se> wrote, quoted or indirectly quoted
someone who said :

>How can I convert a primitive boolean to String?

for all your conversion needs, as the conversion amanuensis.  See
http://mindprod.com/applets/converter.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.