java int comparation

前端 未结 4 1426
自闭症患者
自闭症患者 2021-01-26 02:30

How does Java know/evaluate the comparison of two int values;

for example:

int a=5;
int b=10;

How does it evaluates whethe

4条回答
  •  不要未来只要你来
    2021-01-26 02:46

    If you're looking for how to compare values in code, Stephen C's answer shows how to do just that.

    If you're interested of how the actual machine does the comparison, there's certainly a lot of variety just like djna suggests.

    Here's one way the computer may do the comparison when the Java's correct bytecode as described by Tim is invoked:

    Lets think in binary;

     5 = 0000 0101
    10 = 0000 1010
    

    (Note: I left out some zeros just to keep this simple)

    Since in binary the values are multiples of 2 combined starting from right, it's easy to do a bit comparison;

    • the number which has the highest one bit is larger than other
    • ...if the highest one bit is the same for both, check the one before the highest and keep doing that until either one has zero instead of one OR you run out of bits to check

    Certainly this checking can be done in a bit more fuzzy and definately more complex but better performing way but that's the way it works on the basic machine level.

提交回复
热议问题