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?

Related Posts

  • 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 JavaConclusionWas this post helpful? An object in Java is an instance of a class. It is a […]

  • 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 JavaWas this post helpful? In this post, we will see how […]

  • 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 classesWas this post helpful? 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 […]

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusionWas this post helpful? In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the […]

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusionWas this post helpful? If […]

  • Java projects for beginners
    30 April

    Top 20 Java Projects for Beginners

    This tutorial provides the top 20 java projects for beginners for practice. These projects will help you to learn java quickly and efficiently.

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.