views:

267

answers:

9

What is the most accepted way to convert a boolean to an int in Java?

+13  A: 

Hello,

int myInt = (myBoolean)?1:0 ;

^^

PS : true = 1 and false = 0

Jmix90
parantheses are not necessary here.
Thorbjørn Ravn Andersen
In the case where myBoolean stands for a boolean expression, using parenthesis is more readable.
rsp
more readable? Only to the untrained eye.
Thorbjørn Ravn Andersen
Blrfl
+2  A: 

If true -> 1 and false -> 0 mapping is what you want, you can do:

boolean b = true;
int i = b ? 1 : 0; // assigns 1 to i.
codaddict
+3  A: 
int val = b? 1 : 0;
Grodriguez
already given ...
Jmix90
Judging from the upvotes looks like this is the most accepted way. Acception Grodriguez answer because it was the first one.
hgpc
+3  A: 
boolean b = ....; 
int i = -("false".indexOf("" + b));
Thorbjørn Ravn Andersen
+1, very creative.
codaddict
I can't imagine that this is "the most accepted way". Indeed inventive!
splash
I think this would be better suited as comment, not an answer.
hgpc
It does not compile, at least not in Java: `subString` (uppercase S) is not a method from String, neither is `substring(String)`... it should be `indexOf`!
Carlos Heuberger
`5 - b.toString().length`
KennyTM
@Carlos, good catch.
Thorbjørn Ravn Andersen
@Kenny, requires Java 5 for autoboxing....
Thorbjørn Ravn Andersen
+2  A: 

That depends on the situation. Often the most simple approach is the best because it is easy to understand:

if (something) {
    otherThing = 1;
} else {
    otherThing = 0;
}

or

int otherThing = something ? 1 : 0;

But sometimes it useful to use an Enum instead of a boolean flag. Let imagine there are synchronous and asynchronous processes:

Process process = Process.SYNCHRONOUS;
System.out.println(process.getCode());

In Java, enum can have additional attributes and methods:

public enum Process {

    SYNCHRONOUS (0),
    ASYNCHRONOUS (1);

    private int code;
    private Process (int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }
}
nhnb
+1 for simple if.
Thorbjørn Ravn Andersen
+3  A: 
public interface StackOverflow {
    public String askQuestionAndReturnTheBestAnswer(String question);
}

public class ConvertionUtils {
    public static int convertBooleanToIntInAMostAcceptableWay(StackOverflow stackOverflow, boolean value) {
        assert stackOverflow != null;
        if (value == true) {
            return stackOverflow.askQuestionAndReturnTheBestAnswer(
                    "Which integer value is the most acceptable for boolean \"true\" in Java?");
        } else {
            return stackOverflow.askQuestionAndReturnTheBestAnswer(
                    "Which integer value is the most acceptable for boolean \"false\" in Java?");
        }
    }
}

I think I don't need to post the implementation of StackOverflow interface.

Best Regards
Dr. Dr. Sheldon Lee Cooper

iimuhin
`value == true` is too verbose, just use `value`. Nice answer anyway
Willi
+2  A: 
public int boolToInt(boolean b) {
    return b ? 1 : 0;
}

simple

Adam
+2  A: 

From apache, which has awesome libraries!

http://people.apache.org/~bayard/commons-lang-3.0-snapshot-api/org/apache/commons/lang/BooleanUtils.html#toInteger%28boolean%29

JH
+1 Apache does have awesome libraries!
hgpc
I can't believe they put this method in there.
Willi
+3  A: 

Using the ternary operator is the most simple, most efficient, and most readable way to do what you want. I encourage you to use this solution.

However, I can't resist to propose an alternative, contrived, inefficient, unreadable solution.

int boolToInt(Boolean b) {
    return b.compareTo(false);
}

Hey, people like to vote for such cool answers !

Edit

By the way, I often saw conversions from a boolean to an int for the sole purpose of doing a comparison of the two values (generally, in implementations of compareTo method). Boolean#compareTo is the way to go in those specific cases.

barjak