java int comparation

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

提交回复
热议问题