0

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

Please give an elaborate explanation.

2
  • Are you asking how Java does it behind the scenes? Or are you asking how to do it in code? Commented Nov 9, 2009 at 7:45
  • "Please give an elaborate explanation" just screams homework. If this were related to a real life problem then Mr. unknown would have (or at least should have) put it into context. Commented Nov 9, 2009 at 12:57

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

Comments

3
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.

5 Comments

How does it know that it is grater than if a >b retuns true
Java doesn't "know" anything. The langauge defines the behaviour that will occur when you apply '>' to two integers. As a programmer we need to understand what '<' does, and realise that it conforms to our understanding of "greater". [for extra credit consider what happens in C when you start comparing signed and unsigned quantities.]
Java (JVM) definitely must "know" (that is, implement) what the specification defines. How it does that is quite a valid question.
He asked how does java know that "greater" if a > b returns true. I interpreted that question to be about the semantic significance of "Greater" and the effect of the ">", humans understand the meaning of > and apply it, Java itself doesn't - it just does what the spec says > should do.
Yea ... it is difficult to figure out what the OP is really asking.
2

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.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.