简体   繁体   中英

Java: nice way to convert primitive boolean into primitive int (0,1)

I understand it should be pretty simple but...

  • I have primitive java boolean.
  • I believe such a simple things should not require additional method coding.
  • I strongly dislike ternary operator and inline conditions ( ? for those who want reputation and trying to answer anything even without reading question). So (value ? 1 : 0) is not for me.
  • I cannot use nice fragment like value.compareTo(false) because it is primitive .

What I'd like to get is something like last point but for primitives. Surprised there is no good way to do such a plain useful conversion? Me to. Is your google bigger than my one? ;-D

UPDATE:

OK, finally I've decided to write my own method for this case as there is not only type conversion but some logic that could be extended in future and I'd like to have it encapsulated. Thanks to all people participated.

如果你不能严格使用value.compareTo(false)因为它是原始的,那么怎么样

Boolean.valueOf(value).compareTo(Boolean.FALSE);

What about this?

boolean x = true;

System.out.println(Boolean.compare(x, false));

way number 2 :

System.out.println(5 - Boolean.toString(x).length());

way number 3 (longer):

boolean x = true;

try{
    System.out.println(Boolean.toString(x).charAt(4) - 101);
}catch(StringIndexOutOfBoundsException e){
    System.out.println(1);
}

There is no nice way. An extra function definition isn't that bad. Just define a function if you hate ternary:

int bool_to_int(boolean mybool) {
    if (mybool)
        return 1;
    return 0;
}

Using the ternary operator is the most simple, most efficient, and most readable way to do what you want. I encourage you to use this solution.

However, I can't resist to propose an alternative, contrived, inefficient, unreadable solution.

int boolToInt(Boolean b) {
    return b.compareTo(false);
}

OK, finally this is best thing I've found:

public class Main {

    public static int resultCode(final boolean flag) {
        return Boolean.compare(flag, false);
    }

    public static void main(final String[] args) {
        boolean v1 = false;
        boolean v2 = true;
        System.out.println(v1 + " > " + resultCode(v1));
        System.out.println(v2 + " > " + resultCode(v2));
    }
}

Here's some ideas for your method - thay are all exciting because hey are far more complicated than b ? 1: 0 b ? 1: 0 .

int bi1(boolean b) {
    // The number of items in a set containing both false and the value
    Set<Boolean> s = new HashSet<Boolean>();
    s.add(b);
    s.add(false);
    return s.size() - 1;
}

int bi2(boolean b) {
    // Side-effect usage.
    int bi = 0;
    boolean x = (b && (bi = 1) == 0);
    return bi;
}

int bi3(boolean b) {
    // Using Compareto in a gratuitously obscure way.
    return (int) Math.signum(("" + b).compareTo(Boolean.FALSE.toString()));
}

int bi4(boolean b) {
    // A twiddle too far.
    return ((int) ("" + !b).charAt(0)) >> 1 & 1;
}

int bi5(boolean b) {
    // The old if heave
    if (b) {
        return 1;
    } else {
        return 0;
    }
}

static final List<Boolean> ltf = Arrays.asList(false, true);

int bi6(boolean b) {
    // List twiddling.
    return ltf.indexOf(b);
}

How many more do you want?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM