How to convert boolean values (true,false,undefined) to a string


I need a solution to convert a boolean value to string.
Easy job if there where not those undefined (null) values.

So I need at best a one liner to handle this problem:

boolean value: true -> 'true'
boolean Value: undefined (null),false -> 'false'


LVL 27
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Are you working in a db? A primitive boolean can only have 2 states
Hi there,

Hope that helps,
TolomirAdministratorAuthor Commented:

yes I'm working with a postgreSQL DB and I want to export values with a java tool to an oracle DB (ETL process)

PostgreSQL got that "nice" a boolean can be undefined feature.... causing me problems.

Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

TolomirAdministratorAuthor Commented:
Right now I'm trying something like this:

oracle_tmp.f25string = row1.f25boolean <> NULL ? row1.f25boolean.toString(): 'false';

Try calling the following with resultSet.getObject(booleanFieldIndex)

    public static String boolToString(Object o) {
        return o == null? "null" : ((Boolean)o).toString();
//even the following could do it ;-)
    public static String boolToString(Object o) {
        return "" + o;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TolomirAdministratorAuthor Commented:
The code snipped did it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.