Value vs Reference comparison in Java
Are you currently coding in Java and deciding between using the == operator or the equals() method? Well, let's try to decode this below.
First of all, let’s try to understand how variables or objects are stored in memory. Let’s assume you created the variable:
count = 10
A compartment (location) is created in the computer memory called count. The number 10 is not stored in the compartment per se. The number is translated into computer language (binary) and stored in the compartment.
To understand how these operators work in Java, here’s a bries refresher on the difference between primitive and non-primitive data types.
Primitives vs non-primitives
Note that everything in Java is an object. Objects are composed of primitive and nonprimitive data types. Primitive data types are well, primitive, meaning that they are the original data types we know in programming which are short, int, long, float, double, byte, boolean and char (the first five only hold numeric values for arithmetic manipulations, and the next two hold characters for textual manipulation. The boolean data type is only true or false). Primitive data types are not really considered to be objects by most “Javites” as they are different from Java classes.
Non-primitive data types are strings, arrays, and classes. Non-primitives are data types are defined by the user (except the String Class which is predefined) while primitives are the fundamental classes pre-defined in Java
The == operator
In Java, the == operator compares the values (bits) of primitive data types, and for non-primitive data types, it checks if the object references or memory location are the same. The code below shows how this operator works for primitives.
Here we have created two variables with references called num1 and num2 (this means the compartments in the memory are called num1 and num2 respectively). Obviously, they hold the same value (30). num1 == num2 is asking “do these primitive variables hold the same value?” The answer is yes, so the output should be true.
In the code above, the values of num1 and num2 differ, hence we expect the output to be false
For non-primitive data types, == asks the question “do these references refer to the same object?”
Here we have created two string objects (a non-primitive data type) with references called s1 and s2 (this means the compartments in the memory are called s1 and s2 respectively). Obviously, they hold the exact same content (Ben). The statement s1 == s2 is asking “do these references refer to the same object? Or are these references the same?” The answer is no, so the output should be false. Yes, we know that the object content is exactly the same, but they have different references or memory locations, s1 and s2. The == operator compares the object references (memory location). This is also true for any classes created.
The reason the == gave us the expected result for tests 1 and 2 above is that the number was a primitive data type. For primitive data types,the == operator compares the values, not the memory location.
However, let’s look at something interesting in the code below:
Run the code and see that this time, s1 == s2 is true.
I know you are asking why? What changed? When you look at the declaration and initialization of the string, we removed the “new String”. This is essentially telling the computer to not assign a different memory location to the string objects. To be safe, do not use the == operator for non-primitive data types.
The previous examples may have been confusing. How then can we compare the content of objects you might ask?
This is indeed where the equals() method comes in. The equals() operator asks the question “Are these objects the same? Or Do these objects contain the same values (bits)? See example below:
The code above outputs true because the value or content of s1 and s2 is Medium.
Lets’s see what happens when the String content is different:
The code above outputs false because the value of s1 and s2 outputs false.
In conclusion, if you want to compare primitive data types, use the == operator. However, for non-primitive data types, use the equals() operator.
I hope this helps you further in your code. If you have any questions, corrections or suggestions, let me know in the comments. Thanks for reading. Happy coding!