Is there an easy way to convert a simple integer into a boolean type?

Recommended Answers

All 8 Replies

(i != 0)


boolean b;
int iSomeValue;


or use function

boolean intToBool(int iVal)
 boolean b=true;
 return b;

That is a lot more code than is needed. The conversion only needs the evaluation of intVal!=0, so the function reduces to

boolean intToBool(int value){
  return (value != 0);

Thanks for the help, by th way i used ezzral's method it was quick and simple, i also found out i could've just started with a boolean by setting random generator class to boolean type.


int intVal = (boolVal) ? 1 : 0;

Why do you want to convert a boolean to an int.

It makes no sense. A boolean has 2 values, an int has billions.
Why would one of the billion values be true and another be false?
0 and NOT 0 are C concepts. Nothing in java corresponds.

This thread has been solved but I would also like to add a small suggestion. If for some reason you want to use ints for boolean expressions, why don't you make a class for it. Call it: BoolInt. Have it have one int private attribute and with the help of public set methods make sure that the only valid values are 1 and 0 (or whatever you want)

class BoolInt {
  public static final int INT_TRUE = 1;
  public static final int INT_FALSE = 0;

  public BoolInt() {

  private int i = INT_FALSE;

  public int getIntValue() {
     return i;

  public boolean getBoolValue() {
     return i==INT_TRUE;

  public void setIntValue(int i) throws Exception {
     // if the user doesn't enter 1 or 0 throw exception
     if (i!=INT_TRUE || i!=INT_FALSE) throw new Exception("your message");


  public void setBoolValue(boolean b) {
     i = (b)?INT_TRUE:INT_FALSE
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.