×

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 calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

Tilt operator in Java | Tilde operator in Java Example

The symbol designates it as a unary operator (pronounced as the tilde). It gives back the bit's complement or inverse. Every 0 turns into a 1, and every 1 back...

3 minutes read.

Undo and Redo Operations in Java

Undo and redo operation are the most widely used operation while dealing with file. In this section, we will discuss how to implement undo and redo operation in Java. Undo Redo...

2 minutes read.

Class memory in Java

Anything specifically defined in the Java code is stored either inside the heap or the stack, which is particularly significant. Stack and heap are the two kinds of memory available...

6 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

Java String Inbuilt functions

There are different types of inbuilt functions available in the Java String class, all of them are listed below. Char chat At (int index)It outputs the character indicated by the index....

5 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

Zigzag Traversal of Binary Tree in Java

In this article, you will be acknowledged about the zigzag traversal of binary tree in java and the approaches or ways in which the zigzag traversal can be done. Zigzag Traversal A...

10 minutes read.

How to Convert Hexadecimal to Decimal in Java

How to Convert Hexadecimal to Decimal in Java There are two methods to convert Hexadecimal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method It is a static method of...

2 minutes read.

Map of Map in Java

The map is now a Java interface for mapping keys to values. It is frequently necessary to use Map of Map (nested Map). Nested Maps are useful in various situations, including...

3 minutes read.

Java callable example

What is callable in Java? Callable is an interface that is present in Java. There are two methods for constructing threads: one is to inherit the Thread class, while the other...

3 minutes read.

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) : The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform...

4 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.