3

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.

Roman Nikitchenko
  • 12,800
  • 7
  • 74
  • 110

6 Answers6

2

If you can't use value.compareTo(false) strictly because it's primitive, then how about

Boolean.valueOf(value).compareTo(Boolean.FALSE);
corsiKa
  • 81,495
  • 25
  • 153
  • 204
1

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);
}
gkrls
  • 2,618
  • 2
  • 15
  • 29
  • You'll find that is implemented with two ternary operators: `return (x == y) ? 0 : (x ? 1 : -1);` – corsiKa Aug 26 '14 at 19:58
0

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;
}
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);
}
Jay
  • 2,656
  • 1
  • 16
  • 24
  • 1
    This depends on the implementation of `Boolean.compareTo()` - the spec for `compareTo()` only requires that a positive or negative value is returned on greater-than or less-than comparisons. In the future this could in-theory return anything `> 0` – jdphenix Aug 26 '14 at 19:37
0

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));
    }
}
Roman Nikitchenko
  • 12,800
  • 7
  • 74
  • 110
0

Here's some ideas for your method - thay are all exciting because hey are far more complicated than 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?

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213