2

Possible Duplicate:
Why boolean in Java takes only true or false? Why not 1 or 0 also?

I was wondering today why Java cannot test any other type than a boolean.

In C, C++ and many other languages (actually most programming languages), the following is possible and valid:

int a = 0;
if (a) // evaluates to false
  ; // do something nice

a = 6;
if (a) // evaluates to true
  ; // do something more

This also works almost everywhere for objects, arrays; anything that can have a value of 0x00000000 in the memory.

The question: why is this not possible in Java (you have to keep on testing for == 0 or == null)?

Community
  • 1
  • 1
Hidde
  • 10,623
  • 7
  • 40
  • 65

2 Answers2

0

Because James Gosling et al decided that Java wouldn't do that.

mcfinnigan
  • 10,994
  • 34
  • 28
0

I would guess that the rationale why is because it simplifies things.

An if statement has to evaluate a value to one of two possible conditions. What Java does is require you to supply a statement itself that must evaluate to two possible conditions (boolean) rather than accept other values and arbitrarily decide if that evaluates to true or false.

Michael Berry
  • 66,225
  • 20
  • 144
  • 202