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

前端 未结 3 1108
悲&欢浪女
悲&欢浪女 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条回答
  •  说谎
    说谎 (楼主)
    2020-12-31 06:06

    Java supports widening conversions on primitive numeric types. However, boolean is not considered a numeric type.

    The supported widening conversions are listed under "Widening Primitive Conversion" in the Java Language Specification.

提交回复
热议问题