adams172
adams172

Reputation: 189

How "==" works on primitive types

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

Upvotes: 1

Views: 1722

Answers (3)

Jcross
Jcross

Reputation: 29

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.

Upvotes: 1

Omkar Shetkar
Omkar Shetkar

Reputation: 3636

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.

Upvotes: 0

Kayaman
Kayaman

Reputation: 73568

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.

Upvotes: 9

Related Questions