➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Equal operator in Java with Examples | In Java equal operator is used to compare two primitives based on their value and two objects based on their references. After comparison, it gives a boolean result either true/false. It is widely used in conditional statements. Syntax:-
LHS value == RHS value
Important points on Java Equal operator in Java,
- Equal operator can be used to compare both primitive values, and objects.
- Equal operator compares primitives based on their values, and objects based on their reference.
- The equal (==) operator can’t compare incompatible objects/values, for this we get compile-time error.
- It is an operator and in Java we can’t be override operators.
Example to demonstrate equal operator in Java for primitive values,
public class Test { public static void main(String[] args) { int a = 5; int b = 10; int c = 5; System.out.println(a == b); // false System.out.println(a == c); // true System.out.println(b == c); // false System.out.println('A' == 'A'); // true System.out.println('A' == 'B'); // false System.out.println(true == true); // true System.out.println(false == true); // false } }
Output:-
false
true
false
true
false
true
false
The equal (==) operator can’t compare incompatible values, for this we get the compile-time error. For example- boolean is a primitive data type but it is incompatible with remaining all other remaining data types.
public class Test { public static void main(String[] args) { int a = 5; double d = 20.5; boolean b = true; System.out.println(a == b); System.out.println(d == b); } }
After compilation:-
Test.java:8: error: incomparable types: int and boolean
System.out.println(a == b);
^
Test.java:9: error: incomparable types: double and boolean
System.out.println(d == b);
^
2 errors
While comparing referenced variables, if both variables are referencing to the same object then it gives the result as true, else it gives the result false.
public class Test { public static void main(String[] args) { int[] arr1 = {10, 20, 30, 40, 50}; int[] arr2 = {10, 20, 30, 40, 50}; int[] arr3 = arr1; System.out.println(arr1 == arr2); // false System.out.println(arr1 == arr3); // true } }
Output:-
false
true
If objects are pointing from the same referenced variable then they are equal and the equal operator returns true else it returns false.
public class Test { public static void main(String[] args) { Test t1 = new Test(); Test t2 = new Test(); Test t3 = t1; System.out.println(t1 == t2); // false System.out.println(t1 == t3); // true } }
Output:-
false
true
If we want to compare objects based on their content but not based on their reference then we have to use the equals() method of java.lang.Object class. Also see:- Difference Between == and equals() in Java.
Using an equal operator in Java, We can’t compare incompatible objects else we will get a compile-time error.
public class Test { public static void main(String[] args) { int[] arr = {10, 20, 30, 40, 50}; Test t1 = new Test(); System.out.println(arr == t1); // error } }
After compilation:-
Test.java:5: error: incomparable types: int[ ] and Test
System.out.println(arr == t1); // error
^
1 error
Comparison using == Operator | Valid? | Return |
Two objects | Yes | false |
Two primitive variables | Yes | false/true |
Two nulls | Yes | true |
Null with object | Yes | false |
Object with null | Yes | false |
Null referenced with null | Yes | true |
Null referenced with object | Yes | false |
Object with null referenced | Yes | false |
Incompatible objects | No | error |
Comparing Null using Equal Operator in Java
1) If we compare two nulls with each other using an equal operator then it gives true. Example:-
public class Test { public static void main(String[] args) { System.out.println(null == null); // true } }
Output:-
true
2) If we compare null with null reference variable using equal operator then also it gives true. Example:-
public class Test { public static void main(String[] args) { int[] arr = null; Test t1 = null; System.out.println(arr == null); // true System.out.println(t1 == null); // true } }
Output:-
true
true
3) If we compare null with object reference variable using equal operator then it gives false. Example:-
public class Test { public static void main(String[] args) { int[] arr = {10, 20, 30, 40, 50}; Test t1 = new Test(); System.out.println(arr == null); // false System.out.println(t1 == null); // false } }
Output:-
false
false
4) If we compare null with Objects using an equal operator then also it gives false. Example:-
public class Test { public static void main(String[] args) { int[] arr = {10, 20, 30, 40, 50}; Test t1 = new Test(); System.out.println(null == new Test()); // false } }
Output:-
false
Using == Operator to Compare null with | Return value |
Another null | True |
Null reference variable | True |
Object reference variable | False |
Object | false |
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!