Convert boolean to int in Java

JavaCastingBooleanInt

Java Problem Overview


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

Java Solutions


Solution 1 - Java

int myInt = myBoolean ? 1 : 0;

^^

PS : true = 1 and false = 0

Solution 2 - Java

int val = b? 1 : 0;

Solution 3 - Java

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.

Edit 2

Java 7 introduced a new utility function that works with primitive types directly, Boolean#compare (Thanks shmosel)

int boolToInt(boolean b) {
    return Boolean.compare(b, false);
}

Solution 4 - Java

boolean b = ....; 
int i = -("false".indexOf("" + b));

Solution 5 - Java

public int boolToInt(boolean b) {
    return b ? 1 : 0;
}

simple

Solution 6 - Java

import org.apache.commons.lang3.BooleanUtils;
boolean x = true;   
int y= BooleanUtils.toInteger(x);

Solution 7 - Java

If you use Apache Commons Lang (which I think a lot of projects use it), you can just use it like this:

int myInt = BooleanUtils.toInteger(boolean_expression); 

toInteger method returns 1 if boolean_expression is true, 0 otherwise

Solution 8 - Java

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;
	}
}

Solution 9 - Java

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.

Solution 10 - Java

If you want to obfuscate, use this:

System.out.println( 1 & Boolean.hashCode( true ) >> 1 );  // 1
System.out.println( 1 & Boolean.hashCode( false ) >> 1 ); // 0

Solution 11 - Java

Lets play trick with Boolean.compare(boolean, boolean). Default behavior of function: if both values are equal than it returns 0 otherwise -1.

public int valueOf(Boolean flag) {
   return Boolean.compare(flag, Boolean.TRUE) + 1;
}

Explanation: As we know default return of Boolean.compare is -1 in case of mis-match so +1 make return value to 0 for False and 1 for True

Solution 12 - Java

public static int convBool(boolean b)
{
int convBool = 0;
if(b) convBool = 1;
return convBool;
}

Then use :

convBool(aBool);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhpiqueView Question on Stackoverflow
Solution 1 - JavaJonathan ANTOINEView Answer on Stackoverflow
Solution 2 - JavaGrodriguezView Answer on Stackoverflow
Solution 3 - JavabarjakView Answer on Stackoverflow
Solution 4 - JavaThorbjørn Ravn AndersenView Answer on Stackoverflow
Solution 5 - JavaAdamView Answer on Stackoverflow
Solution 6 - JavaDarkenielView Answer on Stackoverflow
Solution 7 - JavatriForce420View Answer on Stackoverflow
Solution 8 - JavaHendrik BrummermannView Answer on Stackoverflow
Solution 9 - JavacodaddictView Answer on Stackoverflow
Solution 10 - JavaChristian UllenboomView Answer on Stackoverflow
Solution 11 - JavamumairView Answer on Stackoverflow
Solution 12 - JavaRegis_AGView Answer on Stackoverflow