- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The equals and == operator for Enum data type in Java
We have the Devices Enum with four constants.
enum Devices { LAPTOP, MOBILE, TABLET, DESKTOP; }
We have created some objects and assigned them with the constants.
Let us compare them with both equals() and ==. Firstly, begin with equals() −
if(d1.equals(d2)) System.out.println("Devices are the same."); else System.out.println("Devices are different.");
Now, let us move forward and check for ==
if(d1 == d3) System.out.println("Devices are the same."); else System.out.println("Devices are different.");
The following is the final example demonstrating both equals() and == operator −
Example
public class Demo { enum Devices { LAPTOP, MOBILE, TABLET, DESKTOP; } public static void main(String[] args) { Devices d1, d2, d3, d4; d1 = Devices.LAPTOP; d2 = Devices.LAPTOP; d3 = Devices.TABLET; d4 = Devices.DESKTOP; if(d1.equals(d2)) System.out.println("Devices are the same."); else System.out.println("Devices are different."); if(d1 == d3) System.out.println("Devices are the same."); else System.out.println("Devices are different."); } }
Output
Devices are the same. Devices are different.
- Related Articles
- What is MySQL ENUM data type? What are the advantages to use ENUM data type?
- What is the difference between equals() method and == operator in java?
- C# Enum Equals Method
- Java Program to Differentiate String == operator and equals() method
- what are the different attributes of MySQL ENUM data type?
- What is a Type-safe Enum in Java?\n
- How can I insert default value in MySQL ENUM data type?
- Enum for days of week in Java
- What is the type conversion operator ( ) in Java and how to use it?
- Add a new value to a column of data type enum in MySQL?
- Golang Program to Differentiate String == operator and equals() method
- Enum in Java
- Which equals operator (== vs ===) should be used in JavaScript?
- Does SQL Server have an equivalent to MySQL's ENUM data type?
- How do we use an enum type with a constructor in Java?\n

Advertisements