How to compare enum with String in Java

Program to Compare String to Enum Java

Here we are comparing the enum of days to string, this is obviously not equal hence the result will be not equal. The enum is a different type and the string is different while comparing it is not equal. Check the below program.

enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public class Main { public static void main(String[] args) { Main obj = new Main(); obj.checkEnum("TUESDAY"); } private void checkEnum(String string) { Day[] Days = Day.values(); for (Day day : Days) { System.out.println("Day: " + day.name()); if (day.equals(string)) { System.out.println("Equal"); } else { System.out.println("Not equal"); } } } }

Output:-

Day: SUNDAYNot equalDay: MONDAYNot equalDay: TUESDAYNot equalDay: WEDNESDAYNot equalDay: THURSDAYNot equalDay: FRIDAYNot equalDay: SATURDAY

Not equal

How To Compare Enum With String In Java

As we saw in the above program the string and enum cannot be compared directly even if it looks the same as the types are different which might result in inequality, so to overcome this we need to first convert the enum value to string and then compare to do this here we are using toString() method.

enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public class Main { public static void main(String[] args) { Main obj = new Main(); obj.checkEnum("TUESDAY"); } private void checkEnum(String string) { Day[] Days = Day.values(); for (Day day : Days) { System.out.println("Day: " + day.name()); if (day.toString().equals(string)) { System.out.println("Equal"); break; } else { System.out.println("Not equal"); } } } }

Output:-

Day: SUNDAYNot equalDay: MONDAYNot equalDay: TUESDAY

Equal

Now let us see the same thing with the name() method. The name() method yields the same output as the toString() method but the difference is the name() method returns the enum constant that is exactly as same as the one in the declaration. The toString() method returns which are contained in the declaration.

enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public class Main { public static void main(String[] args) { Main obj = new Main(); obj.checkEnum("TUESDAY"); } private void checkEnum(String string) { Day[] Days = Day.values(); for (Day day : Days) { System.out.println("Day: " + day.name()); if (day.name().equals(string)) { System.out.println("Equal"); break; } else { System.out.println("Not equal"); } } } }

Output:-

Day: SUNDAYNot equalDay: MONDAYNot equalDay: TUESDAY

Equal

Get notes to make your learning process easy. These are specially designed for beginners who want to learn coding through simple words, programs, and examples. You can use it as your reference and for revision purposes.

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!

How to compare enum with String in Java

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;

public enum CrunchifyCompany {

GOOGLE, FACEBOOK, YAHOO, TWITTER, CRUNCHIFY

Step-2

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

CrunchifyCompanyEnumTest.java

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) {

// Method-1: Using Switch-Case

private static void crunchifyEnumTestUsingSwitch(CrunchifyCompany company) {

// NOTE: we haven't added switch case for TWITTER intentionally here

log("Enum check PASSED for: GOOGLE");

log("Enum check PASSED for: FACEBOOK");

log("Enum check PASSED for: YAHOO");

log("Enum check PASSED for: CRUNCHIFY");

log("Enum check FAILED for company: " + company);

// 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");

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");

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

If you liked this article, then please share it on social media. Still have any questions about an article, leave us a comment.