Checking the “boolean” result of an “int” type

前端 未结 8 1529
死守一世寂寞
死守一世寂寞 2020-12-10 23:43

I\'m learning Java, coming from C and I found an interesting difference between languages with the boolean type. In C there is no bool/ean

8条回答
  •  心在旅途
    2020-12-11 00:12

    If you insist to use int instead of boolean, just use a method to convert

    class BooleanHelper
    {
       public static boolean toBoolean (int pVal) {
          return pVal != 0;
       }
     ...
    }
    
    // apply
    
    if (BooleanHelper.toBoolean(i)) { // BooleanHelper could be avoided using static imports...
    

    However, just use

    if (i != 0) {
    

    is still shorter and clearer.

提交回复
热议问题