Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how equal operator works with primitive and object type data

I know its a very basic question but I want to be clear about the concept. I want to know how == operator works in case of primitive and object type. For example

Integer a = 1;
int b = 1;
System.out.println(a == b)

how a is compared with b, whereas a contain the ref of object that contains value 1. Can somebody clearify it to me how it works internally?

like image 290
Still Learning Avatar asked Mar 19 '15 07:03

Still Learning


People also ask

When to use the equality operator with primitive types?

When you use the equality operator with primitive types, you’re just comparing their values. Take a look at the following examples: When it comes to object types, the == operator is used to perform a referential equality comparison. What does that mean?

What is the use of equality operator in Java?

In general the equality operator in Java performs a so called shallow comparison. In other words it compares the values that variables contains. Now the variables of primitive data types contains the value itself while the reference types contains reference to heap area which stores the actual content.

What are primitive data types in Java?

Java contains primitive data types for integers, floats, characters, and booleans. These are not objects for efficiency reasons, but everything else in Java is an object belonging to some class So, Java is 99 percent only pure object-oriented. In this section you shall learn the primitive data types and the operators working on them.

What is the difference between primitive and object in JavaScript?

All data types in JavaScript can be put into one of two categories: primitive values and object references. Primitive values and object references behave differently. This difference in behavior affects how variable assignments are made, how equality operators get their results, and how JavaScript programs run in general.


1 Answers

In general the equality operator in Java performs a so called shallow comparison. In other words it compares the values that variables contains. Now the variables of primitive data types contains the value itself while the reference types contains reference to heap area which stores the actual content. That means that in your code snippet int b will hold value 1 and Integer a will hold the memory address of the actual Integer object on the heap.

Now in the particular example provided by you there is one pecularity. Integer class a special wrapper class that wraps primitive integer types. The compiler can automatically convert between such wrapper objects and primitive types (which is called boxing and unboxing).

Let's walk you code step by step tgo make it clear.

Integer a = 1;

The compiler actually substitue the following code insted of this line:

Integer a = Integer.valueOf(1);

The static method valueOf returns an wrapper object instance that wraps the provided primitive value. This procedure when the compiler constructs a wrapper class out of a primitive type is called boxing.

When using such a wrapper object is compared with with a primitive variable using equality operator

a == b

the compiler actually changes it to the following:

a.intValue() == b;

where intValue returns the primitive value wrapped by the wrapper object (which is called unboxing) i.e. it unboxes the primitive value and make the expression equivalent to comparing two primitives. This is why the equality operator then returned true

like image 70
akhilless Avatar answered Sep 30 '22 03:09

akhilless