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.



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.



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.



== in Enum

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


Rating is two


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?

Related Posts

  • 29 November

    How to Get Variable From Another Class in Java

    Table of ContentsClasses and Objects in JavaAccess Modifiers in JavaGet Variable From Another Class in JavaUsing the Default or Public Access Modifier of the Other ClassUsing the Static Member of Another ClassUsing the Inheritance Concept of JavaUsing the Getters and Setters of Another ClassUsing the Singleton Pattern Design for Declaring Global VariablesConclusion In this article, […]

  • 24 September

    Increment for Loop by 2 in Java

    Table of ContentsHow to increment for loop by 2 in JavaHow to increment for loop by n in Java In this post, we will see how to increment for loop by 2 in Java. There are several looping statements available in Java and one of them is for loop in java. There are three parts […]

  • 20 September

    Return ArrayList in Java

    Table of ContentsReturn ArrayList in Java From a Static MethodReturn ArrayList in Java From a Non-static MethodConclusion This article discusses cases of returning an ArrayList in Java from a method. An ArrayList in Java is a collection of elements of the same data type under a single variable name. In different cases, you can return […]

  • Check if Object is null Java
    14 March

    Check if Object Is Null in Java

    Table of ContentsComparison Operator to Check if Object Is Null in JavaisNull() Method to Check if Object Is Null in JavanonNull() Method to Check if Object Is Null in JavarequireNonNull() Method to Check if Object Is Null in JavaConclusion An object in Java is an instance of a class. It is a real entity existing […]

  • How to print multiple variables in java
    31 January

    How to Print Multiple Variables in Java

    Table of ContentsWays to Print Multiple Variables in JavaUsing System.out.printUsing System.out.printfPrint Multiple Variables in Java Using LoggerFrequently Asked QuestionsHow Do I Print Multiple Values on One Line in JavaHow to Print Multiple Integers in JavaHow to Print String and Integer in Same Line in Java In this post, we will see how to print multiple […]

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classes In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction that you can […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.