×

Difference between = = and equals ( ) in java

Java :

Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems in the year 1996 as java 1.0 . It is a high level, robust, secured programming language.

Java consists of Java Virtual Machine (JVM) which uses the execution engine and converts the class file into executable file (.class file to .exe file).

The execution engine consists of Interpreter and Just In Time(JIT) compiler. In Java we will create classes and then we will create objects.

There are many types of variables , methods , operators , keywords and data types in java. There are primitive data types and non-primitive data types or primary data types and secondary data types in java.

Some such kind of operator is “ = = “operator and some such kind of method is called “ equals ( ) “ method.

Let us know about these “ = = “ operator and “ equals ( ) “ method in java in detail with the examples.

Equality operator ( = = ) :

The symbol “ = = “ is an operator. It is also known as equality operator. It is used in various ways in various languages. It is also used in C language to check whether the right hand side value is equal to the left hand side value or not but in java language the usage of equality operator ( = = ) is totally different from C language.

In Java language the equality operator ( = = ) checks whether the both variables are having same reference or not. It also checks whether the reference of both variables locate to the same point or not.

It checks completely about the memory areas that is it checks that the reference is pointing to the same location or not. The equality operator ( = = ) will not support over riding. It does not matter about the content in the variable it only checks the references of the variable.

We can check any kind of variables using equality operator. We can check string data type variables or integer data type variables or float data type variables or variables of any data type.

By using the equality operator ( = = ) if the references of both the variables is same or located to the same memory area or located to the same point then it will return true else it returns false in the output. The output is in the form of Boolean data type that is true or false. 

Syntax to use equality operator ( = = ) :

The syntax to use equality operator ( = = ) is as shown below :

variable1 = = variable2

Let us look over an example using the following code to learn more about the equality operator ( = = ) as shown below :

Example:

import java.io.*;
class Demo
{
public static void main ( String args [ ] )
{
String a = “ hellojava “;
String b = “ hellojava “;
String c = new String( “ Hello Java “ );
System.out.println ( a = = b ); // checking reference
System.out.println ( b = = c ); // checking reference
System.out.println ( 10 = = 10 ); // integer data type ( reference )
System.out.println ( ‘a’ = = ‘a’ ); // character data type ( reference )
} // main method
} // demo class

Output :

true
false
true
true

In the above example we have used the equality operator ( = = ). We have checked the reference of three strings in the above program. String can be used as both the data type as well as class format. In such cases we use the concept of string constant pool so that we can handle strings in different ways.

If the strings are read in data type format then the reference is located to class area or the memory is created under class area.

If the strings are read in the class format then the reference is located to heap area or the memory is created under heap area. Because in class in order to create an object we will use new operator which will create the memory under heap area.

Here in this program we have created the strings a and b using the string as a data type but the string c is created by using they string as a class. Therefore the references of the strings a and b are located at class area and the reference of c is located at heap area.

Hence the output is as shown above in the output.

Equals ( ) method :

The symbol equals ( ) is a method. The method equals ( ) is a method of the object class. It means that the method is a built in method under the object class. It is used in various ways in various languages.

It is also used in C # language to check whether the right hand side value is equal to the left hand side value that is whether the two string objects are having the same value or not . But in java language the usage of equals ( ) method is totally different from C # language.

In Java language the equals ( ) method checks whether the both variables are having same content or not. Compare to equality operator ( = = ) which is used to check the reference of the variables the equals ( ) method is used to check the value of the variables or the content of the variables.

It checks completely about the content of the variables and it does not checks the memory areas or the reference of the variables is pointing to the same location or not. If there is no equals ( ) method declared then it will utilize the Object.equals ( ) method or else it will be over riden. It does not matter about the reference of the variables it only checks the content of the variables.

We can use equals ( ) method in many kind of data types. By using the equals ( ) method if the content of both the variables is same then it will return true else it returns false in the output. The output is in the form of Boolean data type that is true or false. 

Syntax to use equals ( ) method :

The syntax to use equality operator ( = = ) is as shown below :

variable1 . equals ( variable2 )

Let us look over an example using the following code to learn more about the equals ( ) method as shown below :

Example :

import java.io.*;
class Demo
{
public static void main ( String args [ ] )
{
String a = “ hellojava “;
String b = “ hellojava “;
String c = new String( “ Hello Java “ );
System.out.println ( a . equals ( b ) ); // checking content
System.out.println ( b . equals ( c ) ); // checking content
System.out.println ( 10 . equals ( 10 ) ); // integer data type ( content )
System.out.println ( ‘a’ . equals ( ‘a’ ) ); // character data type ( content )
} // main method
} // demo class

Output :

true
false
true
true

In the above example we have used the equals ( ) method. We have checked the contents of three strings in the above program. String can be used as both the data type as well as class format. In such cases we use the concept of string constant pool so that we can handle strings in different ways.

If the strings are read in data type format then the reference is located to class area or the memory is created under class area.

If the strings are read in the class format then the reference is located to heap area or the memory is created under heap area. Because in class in order to create an object we will use new operator which will create the memory under heap area.

Here in this program we have created the strings a and b using the string as a data type but the string c is created by using they string as a class. Therefore the references of the strings a and b are located at class area and the reference of c is located at heap area but whatever the reference may be the content of the strings should be same.

Here we have the same content in the strings a and b but the content in the string c is different from the content of the strings a and b. Therefore it prints true when we check the content using equals ( ) method for the strings a and b and it prints false when we check the content using equals ( ) method for the strings b and c.

Hence the output is as shown above in the output.

Now let us see the example of using both equals ( ) method and the equality operator ( = = ) using the following code to show the difference between equality operator and equals ( ) method as shown below :

Example :

import java.io.*;
class Demo
{
public static void main ( String args [ ] )
{
String a = “ hellojava “;
String b = “ hellojava “;
String c = new String( “ Hello Java “ );
System.out.println ( a = = b ); // checking reference
System.out.println ( b = = c ); // checking reference
System.out.println ( a . equals ( b ) ); // checking content
System.out.println ( b . equals ( c ) ); // checking content
} // main method
} // demo class

Output :

true
false
true
false

Related Topics

How to Create Different Packages for Different Classes in Java

Packages in Java In Java, Packages are an assortment of classes, sub-packages, and connection points. i.e. A package addresses a word reference that contains a connected gathering of styles and points...

7 minutes read.

Java Lock

A lock is indeed a threaded synchronization technique similar to Java's synchronized blocks, however, locking can be more complex. It's not like we can completely get rid of the synchronized...

5 minutes read.

ArrayList VS Linked List

ArrayList VS Linked List Both the ArrayList and LinkedList implements the List interface, and they have some differences as well as some similarities between them. The internal working and performance of both vary significantly. Let’s...

7 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

Java Math floor() Method

The floor() method of Math class returns the largest double value that is equal to a mathematical integer and is less than or equal to the argument. Syntax: public static double floor(double...

2 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

Java Math max() Method

The max() method of Math class returns the greater of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double max(double a, double b)public...

2 minutes read.

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.

Java Get Time in UTC

UTC is the abbreviation for Universal Time Coordinated. Before the beginning of UTC, it is mentioned as the Greenwich Mean Time (GMT) but Now it is mentioned as the universal...

4 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.

How to get Day Name from Date in Java

We'll write a Java application to extract the day's name from the Date in this section. When dealing with Date and time in Java, the following classes are used. Class for Calendars:...

6 minutes read.

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These...

5 minutes read.

Java Project Ideas

When it comes to constructing projects, Java is regarded as one of the best languages and is also one of the most paid. Java excels in any application, whether it...

10 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

What is interpreter in Java?

The programming language Java is platform-neutral. Therefore, can use Java on any platform that supports the Java processor. The Piece of software transforms the Java bytecode contained in the class...

5 minutes read.

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

Generic Linked List in Java

A linear data structure known as a Linked List stores values in nodes. As we already know, each node has two properties: its value and a link to the node...

6 minutes read.