tags:

views:

457

answers:

6

is there any reason why java booleans take only true or false why not 1 or 0 also?

+14  A: 

Because booleans have two values: true or false. Note that these are not strings, but actual boolean literals.

1 and 0 are integers, and there is no reason to confuse things by making them "alternative true" and "alternative false" (or the other way round for those used to Unix exit codes?). With strong typing in Java there should only ever be exactly two primitive boolean values.

EDIT: Note that you can easily write a conversion function if you want:

public static boolean intToBool(int input)
{
   if (input < 0 || input > 1)
   {
      throw new IllegalParameterException("input must be 0 or 1");
   }

   // Note we designate 1 as true and 0 as false though some may disagree
   return input == 1;
}

though I wouldn't recommend this. Note how you cannot guarantee that an int variable really is 0 or 1; and there's no 100% obvious semantics of what one means true. On the other hand, a boolean variable is always either true or false and it's obvious which one means true. :-)

So instead of the conversion function, get used to using boolean variables for everything that represents a true/false concept. If you must use some kind of primitive text string (e.g. for storing in a flat file), "true" and "false" are much clearer in their meaning, and can be immediately turned into a boolean by the library method Boolean.valueOf.

Andrzej Doyle
+1: The same reason "x" is not a number, and 123 is not a date.
S.Lott
Being specific about this keeps you away from the whole TRUE in VB is -1 and in other langauges true is just NON ZERO. Keeping the boolean field as true or false keeps java outside of this argument.
Shaun F
+6  A: 

Java, unlike languages like C and C++, treats boolean as a completely separate data type which has 2 distinct values: true and false. The values 1 and 0 are of type int and are not implicitly convertible to boolean.

JaredPar
At the JVM level, boolean, byte, short, char, and int are all treated as ints, and boolean is indeed represented as 0 and 1. So the "not implicitly convertible" is at the Java language level only.
Chris Jester-Young
Plus, Java overtly attempts to learn from the past so that entire categories of bugs are removed. e.g. this ranges from the small features (no implicit boolean expressions) to the large (garbage collection)
Michael Easter
To continue on from Chris's point, it is legal to write char x = (char) 88945; or char y = (char) -489;.
Lord Torgamus
C didn't originally have a bool type, so you had to use 0 or 1.
R. Bemrose
You didn't really answer the question. They asked *why*.
+3  A: 

Because the people who created Java wanted boolean to mean unambiguously true or false, not 1 or 0.

Nathan Hughes
+1  A: 

Because Java is a strong typed programming language. PHP and Javascript for example are weak typed.

BalusC
A: 

Being specific about this keeps you away from the whole TRUE in VB is -1 and in other langauges true is just NON ZERO. Keeping the boolean field as true or false keeps java outside of this argument.

Shaun F
A: 

On a related note: the java compiler uses int to represent boolean since JVM has a limited support for the boolean type.See Section 3.3.4 The boolean type.

In JVM, the integer zero represents false, and any non-zero integer represents true (Source : Inside Java Virtual Machine by Bill Venners)

sateesh