Why Doesn't Java Allow Casting Boolean -> Int?

前端 未结 3 1122
悲&欢浪女
悲&欢浪女 2020-12-31 05:28

I was wondering why Java doesn\'t allow casting from a boolean to an int, like so:

boolean foo = true;
int bar = (int)foo;

This can be done

3条回答
  •  萌比男神i
    2020-12-31 06:32

    Because Java is strongly typed, a boolean and an int are two completely different data typesand one can not be casted to the other - it can not be casted, in fact.

提交回复
热议问题