How does Java know/evaluate the comparison of two int
values;
for example:
int a=5;
int b=10;
How does it evaluates whethe
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.
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 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;
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.
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:
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:
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.