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