Program to Compare String to Enum JavaHere 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. Show Output:- Day: SUNDAYNot equalDay: MONDAYNot equalDay: TUESDAYNot equalDay: WEDNESDAYNot equalDay: THURSDAYNot equalDay: FRIDAYNot equalDay: SATURDAY Not equal How To Compare Enum With String In JavaAs 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!
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.
Also if you have below questions then you are at right place.
Let’s get started:Step-1Create eNum CrunchifyCompany.java in Eclipse IDE.
Step-2Create class CrunchifyCompanyEnumTest.java which implements comparison methods using equals (==) and using switch case.
Step-3Run the program to see result.
If you liked this article, then please share it on social media. Still have any questions about an article, leave us a comment. |