What is == in java

What is == in java

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.

(MALE == FEMALE) returns false

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.

Output:

true

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.

Output:

true

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.

Output:

false

== in Enum

You can use == to check enum equality.
Here is an example:

Output:

Rating is two

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *