1

Is there a performance difference between == and > / < operations in java specifically.

I have already read this answer, but it doesn't say in terms of java nor the comparison is in between similar operands.

Shiva kumar
  • 673
  • 8
  • 23
  • 1
    It depends a fair bit on the type. – Bathsheba Jan 22 '19 at 08:06
  • 1
    What do you mean by "comparison is in between similar operands." ? – Thilo Jan 22 '19 at 08:08
  • https://stackoverflow.com/questions/6178761/is-there-any-performance-difference-between-greater-than-and-greater-than-or-equ In the above link, its about > and >=, so there might not be much difference. But when it comes to == and > or == and <, would there be a difference is my question. – Shiva kumar Jan 22 '19 at 09:20

1 Answers1

2

As multiple answers already stated, the operators (and all instructions) are converted to machine-understable language (usually, Assembly or Byte code work as a middle: your code is translated to those, then to binary. However, I'm not really working at/used to this level of programming. Feel free to correct me if I'm missing some point / machine language.)

Java compiles its classes to Byte code, which is then translated to machine code (binary) to perform hardware operations and computations. You can even look the byte code by yourself, if it is really needed.


Considering the operators themselves, practically any microprocessor nowadays is able to optimize and make this kind of operations nearly instantaneous. If there was any difference between == and > / < in terms of performance, it would be in the order of mere nanoseconds, being Java, C++ or any compiled language. I am not even sure you could perform "in-code" metrics about this level of performance, considering Java, coupled with hardware, won't always be able to give you precise time measurement when it comes to nanoseconds.

Yassine Badache
  • 1,810
  • 1
  • 19
  • 38