开发者

java int comparation

开发者 https://www.devze.com 2022-12-11 01:50 出处:网络
How does Java know/evaluate the comparison of two int values; for example: int a=5; int b=10; How does it evaluates whether a > b or a == b or a < b

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

for example:

int a=5;
int b=10;

How does it evaluates whether a > b or a == b or a < b

Ple开发者_运维问答ase give an elaborate explanation.


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


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消