1

What is the ratio of processing power used to analyze this:

boolean: true != false

versus this:

int: 1 != 0
sdkfasldf
  • 607
  • 6
  • 19

3 Answers3

4

The answer is JVM and application specific, though I expect you will see no measurable difference. (I say that it is application specific because the surrounding context could well influence the way that the JIT compiler is able to generate optimal native code.)

Seriously, if you are considering micro-optimizing at this level, you are almost certainly wasting your time. The JIT compiler can do a better job than you can, and you might even find that rewriting your code to use int instead of boolean inhibits optimization.

The most productive strategy is to just write your code so that it is readable, and focus on the design of your algorithms and data structures. Only micro-optimize if your implemented code is too slow, and when you do decide to do it, use profiling to tell you where to focus your efforts.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Assuming these are variables, not constants, probably no difference at all.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

They are effectively the same cost, but the real answer is that it is dependent on the virtual machine, etc. If the JVM packs booleans (as this question answer might suggest for boolean arrays: What is the size of a boolean variable in Java?) then accessing a boolean might require some additional arithmetic to mask off the important bit / byte. However, it will usually be the same cost.

Community
  • 1
  • 1
Alex Reece
  • 1,906
  • 22
  • 31