Table of Contents
In this tutorial, we will learn what is == in Java as it is a common lexeme
that we will use while developing applications.
The ==
lexeme can be confused with =
which is another lexeme of the Java language and we will also learn in detail how the two differ to avoid issues such as logical errors in our applications.
We will also learn the difference between the ==
lexeme and the Java method equals()
that uses the ==
lexeme behind the scenes to realize a different functionality.
What is meaning of == in Java
We mentioned above that ==
is a lexeme and a lexeme is a sequence of alphanumeric characters consisting of letters, digits, or punctuation and formed using a regular expression.
The string formed by the sequence of characters describes the construct of a programming language features such as identifiers, keywords, and operators.
In Java, there are several operators that can be used by the language such as arithmetic operators, logical operators, relational operators, and many others depending on the problem at hand.
These operators are represented by individual lexemes for the specific operations and the ==
lexeme is an example of an operator for the relational operators in Java.
This operator is pronounced as equal to
and note that we must use the ==
instead of =
when we want to test whether two operands of the primitive
type are equal.
Code example of ==
The operator is used with primitive data types such as int
, short
, long
, float
, and double
in Java.
The operator compares the two operands and returns a boolean value of true if they are equal and false otherwise.
The following is an example of the ==
operator that compares two operands of type integer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.java2blog; public class EqualityOperator { public static void main(String[] args) { int numOne = 1; int numTwo = 1; System.out.println(numOne ==numTwo); } } |
Output:
Object equality using equal to ==
In Java, we can use the ==
operator to compare whether two objects have the same address and the following is an example that compares two strings.
When we create multiple strings in Java without creating new objects using the new
keywords, the collection of strings is stored in the same location which is the reason that the ==
operator returns true when comparing two string objects.
When comparing custom objects created from custom classes the case is different depending on whether the objects being compared were instantiated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.java2blog; public class EqualityOperator { public static void main(String[] args) { String firstName = "john"; String lastName = "john"; System.out.println(firstName == lastName); } } |
Output:
The difference between == and equals
The ==
operator checks the reference of two variables as demonstrated with the above example and in this section, we will learn how ==
differs from equals.
Equals is a Java method that we can use to test the equality of two objects based on their contents and to achieve this we must override the equals()
method in the class.
Also, note that an equals method is accompanied by a hashcode()
method to store objects with the same hashcode in one bucket and this prevents returning the same objects as unique.
We will use a product object that overrides the equals()
method and uses id in a hashcode to identify the same objects.
The equals method works similarly to the ==
operator if the hashcode is not overridden in the products class and we should take note of it to avoid errors in our applications.
If you are not aware about concept of hashcode() and equals() method, you should go through hashcode() and equals() method in java.
The following example demonstrates the usage of equals that compares two product objects with different id’s which implies different hashcode and a boolean result of false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package com.java2blog; import java.util.Objects; public class EqualityOperator { public static void main(String[] args) { Product bookOne = new Product(1, "Introduction to Java", 60.00, "Basics of Java"); Product bookTwo = new Product(2, "Advanced data structures", 70.00, "advanced data structures"); System.out.println(bookOne.equals(bookTwo)); } } class Product{ private int id; private String productName; private Double productPrice; private String productDescription; public Product(int id, String productName, Double productPrice, String productDescription) { this.id = id; this.productName = productName; this.productPrice = productPrice; this.productDescription = productDescription; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Product product = (Product) o; return id == product.id; } @Override public int hashCode() { return Objects.hash(id); } } |
Output:
Further reading:
== in Enum
You can use == to check enum equality.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog; public class EnumEquality { enum RATING { ONE, TWO, THREE } public static void main(String[] args) { RATING r = RATING.TWO; if(r == RATING.TWO) { System.out.println("Rating is two"); } } } |
Output:
Conclusion
In this tutorial, we have learned what is the meaning of the ==
operator, how the operator can be used to compare string objects and custom objects, and finally looked at the difference between the equal to operator ==
and the Java equals()
method.
That’s all about what is == in java.