How do Java’s equality tests work between primitives? I’m specifically looking for efficiency (and speed) within single-type comparisons. Is the char == char
comparison faster than int == int
?
I know that behind the hood, char
s are treated as numbers. I’m having difficulty finding any explanations on how ==
truly functions with primitives (it understandably doesn’t have a JavaDoc/Oracle page, and I can’t find its source code). The only data I found that can be tangentially related is that int
uses 4 bytes, while char
uses 2. (I don’t need the extra size; all the data I’m comparing would be a real-world character, and Unicode can be represented in Java’s 2-byte char
, unlike C’s 1-byte for ASCII.)
Is the speed the same, or is char
faster due to its smaller size?
I understand that the base code for its operations may be derived from C or even assembly. If C (or equivalent) or higher, where would I be able to find it to further understand how it works?