java int comparation

前端 未结 4 1381
自闭症患者
自闭症患者 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:35

    Java depends upon the capabilities of the CPU, which will have instructions allowing conditional execution paths depending upon the the result of comparing two registers.

    Now there's a further level of abstraction given that Java is executing in a Virtual Machine.

    So to research your answer further: first read up on CPUs, registers, branching, etc. Then read about the Java JVM.

    0 讨论(0)
  • 2021-01-26 02:37

    The JVM interprets instructions such as

    icmpge (Compare Greater than or Equal To)
    

    To do comparisons. It is up to the platform specific JVM how this is performed on your platform of choice.

    So you could say that comparison is native to the Virtual Machine

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-26 02:49
    if (a > b) {
        // a is bigger
    } else if (a == b) {
       // they are equal 
    } else {
       // a is smaller.
    }
    

    EDIT: In answer to this follow up question:

    How does it know that it is grater than if a >b retuns true

    It depends what you mean by "it".

    • Java (the programming language) doesn't know anything. It is a language that has meaning ... not a thing capable of knowledge, in any sense.

    • Javac (the compiler) translates the Java code into a sequence of JVM bytecodes that mean the same thing as the program source code. Those byte codes say something like this:

      1. load the integer value of 'b' to the expression stack
      2. load the integer value of 'a' to the expression stack
      3. branch if the integer on the top of the expression stack is greater than the next integer on the stack
    • Java (the command) interprets the bytecodes, or JIT compiles them to native code, and then executes the native code.

    • At the native code level, a sequence of instructions might do something like this:

      1. load 32 bits from the address of 'a' to register 1
      2. load 32 bits from the address of 'b' to register 2
      3. subtract register 1 from register 2
      4. branch if the result of the subtraction was negative
    • The subtraction is performed at the hardware level using an ALU built from logic gates

    • And so on down to the level of the silicon.

    0 讨论(0)
提交回复
热议问题