1

I wonder how "==" operator works on primitive values. I understand that "==" checks if two references refers to the same object on heap. But how it works in context of primitive values wihich are stored on stack? e.g

int a = 5; int b = 5;

I assume that these values aren't stored in the same place in memory but a == b returns "true".

My hypotesis is that JVM treats all values stored in stack as stored in one place in memory and it returns true in case of "==". Could you explain me this matter in a little more detailed way?

Regards

trincot
  • 317,000
  • 35
  • 244
  • 286
adams172
  • 189
  • 1
  • 10
  • Can you be clearer with what exactly you want to know? Your question is very vague. – 4castle Aug 15 '16 at 13:36
  • It compares them by value. – khelwood Aug 15 '16 at 13:38
  • Possible duplicate : http://stackoverflow.com/questions/1586223/how-does-the-tostring-equals-object-methods-work-differently-or-similar – Vijayendra Nigam Aug 15 '16 at 13:48
  • 5
    You're making things too complicated. `==` simply compares the values. There's nothing going on with comparing addresses or pretending as if values are stored in one place in memory. – Jesper Aug 15 '16 at 14:18

3 Answers3

9

Your hypothesis is wrong. When comparing primitives there are no memory addresses in play. It's a simple instruction to compare whether a value is equal to another value, implemented in bytecode as a comparison (of a register value) and a conditional jump.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
1

Edit: As pointed out by James and Varun, my original answer would not apply to the OP's example since an object reference is a numerical value just like an integer. So the comparison operation would actually be the same in this case.


In other languages there is something called operator overloading. Java does not support user operator overloading, but understanding how those work will give you a better idea how the same operator can use different logic depending on the context.

when you are comparing a variable and a primitive, the machine code that is produced is performing a different comparison operation than when comparing two objects. The same syntax is used because the idea of comparison is logically similar enough to warrant using the same operator symbol rather than defining a completely different symbol for the two different functionalities.

Jcross
  • 29
  • 7
  • 2
    Deep down in processor it is just a comparison instruction.now, different addressing modes of CMP opcode are possible depending on cpu architecture. In very basic mode a frame pointer with index (respective to stack variable) would be used to read both variables and a jump equal or jump not equal opcode would be generated. – Varun Mishra Aug 15 '16 at 13:45
  • 1
    Actually, the implementation of `==` for integer-like types is virtually identical to how it is implemented for object references because an object-reference _is_ just a number. – Solomon Slow Aug 15 '16 at 14:35
  • James and Varun you are both correct. I will edit my answer to reflect that. – Jcross Aug 15 '16 at 14:42
0

For any comparison operation, JVM looks for data type of the operands. Depending on the operand type different Java byte code instructions are used for comparison.

JVM works on two kinds of data types.

According to JVM 7 specification:

Like the Java programming language, the Java Virtual Machine operates on two kinds of types: primitive types and reference types. There are, correspondingly, two kinds of values that can be stored in variables, passed as arguments, returned by methods, and operated upon: primitive values and reference values.

References too are values of type reference.

The Java Virtual Machine contains explicit support for objects. An object is either a dynamically allocated class instance or an array. A reference to an object is considered to have Java Virtual Machine type reference. Values of type reference can be thought of as pointers to objects. More than one reference to an object may exist. Objects are always operated on, passed, and tested via values of type reference.

For arithmetic operations on primitive and reference types, JVM uses opcodes which specify data type of the arguments along with operation.

For example, lcmp - Compare two long values

Finally, for JVM any arithmetic operation is byte arithmetic on given operands.

Omkar Shetkar
  • 3,488
  • 5
  • 35
  • 50