Java eNum Comparison using Equals (==) operator, Switch-Case statement and .equals() method – Complete Tutorial

Last updated
App Shah
Crunchify » Java and J2EE Tutorials » Java eNum Comparison using Equals (==) operator, Switch-Case statement and .equals() method – Complete Tutorial

Java eNum Comparison using equals(), == operator and Switch statement Example

Sometime back I’ve written an article on Java eNum – Why and for what should I use Java eNum?

What is an eNum type?

It is a special data type to which we could assign predefined constants to variable.

In this tutorial we will go over how to compare eNums in your production application. As previous tutorial gives you complete example and details on eNum, this tutorial provides complete insight on comparison part.

There are three ways we could compare eNum in Java.

  1. Switch-Case Statement
  2. == Operator
  3. .equals() method

Also if you have below questions then you are at right place.

  • Comparing Java enum members
  • Use == (or !=) to Compare Java Enums
  • Java.lang.Enum.compareTo()
  • How to Compare Two Enum in Java
  • == or equals with Java enum
  • enum comparison in java
  • enum comparison string java

Let’s get started:

Step-1

Create eNum CrunchifyCompany.java in Eclipse IDE.

package crunchify.com.tutorial;

/**
 * @author crunchify.com
 * 
 */

public enum CrunchifyCompany {

	GOOGLE, FACEBOOK, YAHOO, TWITTER, CRUNCHIFY

}

Step-2

Create class CrunchifyCompanyEnumTest.java which implements comparison methods using equals (==) and using switch case.

package crunchify.com.tutorial;

/**
 * @author crunchify.com, version: 1
 * 
 */

public class CrunchifyCompanyEnumTest {
	public static void main(String[] args) {

		// Let's check eNum using Switch-Case statement
		log("Comparison using Switch-Case ========================");
		crunchifyEnumTestUsingSwitch(CrunchifyCompany.FACEBOOK);
		crunchifyEnumTestUsingSwitch(CrunchifyCompany.TWITTER);

		// Let's check eNum using Equal (==) operator
		log("\nComparison using == operator ========================");
		crunchifyEnumTestUsingEqualSign(CrunchifyCompany.CRUNCHIFY);
		crunchifyEnumTestUsingEqualSign(CrunchifyCompany.YAHOO);

		// Let's check eNum using .equals() method
		log("\nComparison using .equals() ========================");
		crunchifyEnumTestUsingEqualsMethod(CrunchifyCompany.FACEBOOK);
		crunchifyEnumTestUsingEqualsMethod(CrunchifyCompany.GOOGLE);
	}

	private static void log(String str) {
		System.out.println(str);
	}

	// Method-1: Using Switch-Case
	private static void crunchifyEnumTestUsingSwitch(CrunchifyCompany company) {

		// NOTE: we haven't added switch case for TWITTER intentionally here
		switch (company) {
		case GOOGLE:
			log("Enum check PASSED for: GOOGLE");
			break;
		case FACEBOOK:
			log("Enum check PASSED for: FACEBOOK");
			break;
		case YAHOO:
			log("Enum check PASSED for: YAHOO");
			break;
		case CRUNCHIFY:
			log("Enum check PASSED for: CRUNCHIFY");
			break;
		default:
			log("Enum check FAILED for company: " + company);
			break;
		}

	}

	// Method-2: Using == Operator
	private static void crunchifyEnumTestUsingEqualSign(CrunchifyCompany company) {

		// NOTE: we haven't added switch case for GOOGLE intentionally here
		if (company == CrunchifyCompany.FACEBOOK) {
			log("Enum check PASSED for: FACEBOOK");
		} else if (company == CrunchifyCompany.TWITTER) {
			log("Enum check PASSED for: TWITTER");
		} else if (company == CrunchifyCompany.YAHOO) {
			log("Enum check PASSED for: YAHOO");
		} else if (company == CrunchifyCompany.CRUNCHIFY) {
			log("Enum check PASSED for: CRUNCHIFY");
		} else {
			log("Enum check FAILED for company: " + company);
		}
	}
	
	// Method-3: Using equals()
	private static void crunchifyEnumTestUsingEqualsMethod(CrunchifyCompany company) {
		if (company.equals(CrunchifyCompany.FACEBOOK)) {
			log("Enum check PASSED for: FACEBOOK");
		} else {
			log("Enum check FAILED for company: " + company);

		}
	}

}

Step-3

Run the program to see result.

Comparison using Switch-Case ========================
Enum check PASSED for: FACEBOOK
Enum check FAILED for company: TWITTER

Comparison using == operator ========================
Enum check PASSED for: CRUNCHIFY
Enum check PASSED for: YAHOO

Comparison using .equals() ========================
Enum check PASSED for: FACEBOOK
Enum check FAILED for company: GOOGLE

4 thoughts on “Java eNum Comparison using Equals (==) operator, Switch-Case statement and .equals() method – Complete Tutorial”

    • Hi Vishesh –
      The most common metric for calculating time complexity is Big O notation.

      I believe for all above cases – worst complexity is O(n). For best case it’s O(1).

      Reply

Leave a Comment