Converting Enum into String and String to Enum in Java is becoming a common task with growing use of Enum. Enum is very versatile in Java and preferred choice to represent bounded data and since is almost used everywhere to carry literal value its important to know how to convert Enum to String in Java. In this article we will see both first converting Strings to Enum in Java and than Changing an Enum to String in Java with Example. I thought about this Enum tutorial when I wrote 10 Examples of Enum in Java. I missed String to Enum conversion and one of reader pointed out that. So here we have now.
Enum to String to Enum in Java
This article is in continuation of other conversion related post e.g. how to convert Date to String in Java and How to Convert String to Integer in Java. As these are common needs and having best way to do things in mind saves lot of time while coding.
Convert Enum to String in Java Example
Enum classes by default provides valueOf (String value) method which takes a String parameter and converts it into enum. String name should match with text used to declare Enum in Java file. Here is a complete code example of String to Enum in JavaCode Example String to Enum:
public class EnumTest {
private enum LOAN {
HOME_LOAN {
@Override
public String toString() {
return "Always look for cheaper Home loan";
}
},
AUTO_LOAN {
@Override
public String toString() {
return "Cheaper Auto Loan is better";
}
},
PEROSNAL_LOAN{
@Override
public String toString() {
return "Personal loan is not cheaper any more";
}
}
}
public static void main(String[] args) {
//Exmaple of String to Enum in Java
LOAN homeLoan = LOAN.valueOf("HOME_LOAN");
System.out.println(homeLoan);
LOAN autoLoan = LOAN.valueOf("AUTO_LOAN");
System.out.println(autoLoan);
LOAN personalLoan = LOAN.valueOf("PEROSNAL_LOAN");
System.out.println(personalLoan);
}
Output:
Always look for cheaper Home loan
Cheaper Auto Loan is better
Personal loan is not cheaper any more
Convert Enum to String in Java Example
Now let's do opposite convert an Enum into String in Java, there are multiple ways to do it one way is to return exact same string used to declare ENUM from toString() method of Enum, otherwise if you are using toString() for othre purpose than you can use default static name() method to convert an Enum into String. Java by default adds name() method into every Enum and it returns exact same text which is used to declare enum in Java file.
Code Example Enum to String
public static void main(String[] args) {
//Exmaple of Enum to String in Java
String homeLoan = LOAN.HOME_LOAN.name();
System.out.println(homeLoan);
String autoLoan = LOAN.AUTO_LOAN.name();
System.out.println(autoLoan);
String personalLoan = LOAN.PERSONAL_LOAN.name();
System.out.println(personalLoan);
}
Output:
HOME_LOAN
AUTO_LOAN
PERSONAL_LOAN
That’s all on How to convert Enum to String and than back from String to Enum in Java. This tip will help you to quickly convert your data between two most versatile types Enum and String in Java. If you know any other way to change String to Enum in java than please let us know.
Related post:
6 comments:
Good conversion tutorial.10 Exception Handling Interview Questions
Thanks for you Comment Sandeep. Good to know that you like This Enum to String Conversion tip.
Thanks and nice article. Explained nicely.
Java Enum Tutorial
Nice Explaination
Thanks for you comment Kiran, Good to know that you like this Enum to String conversion tutorial.
hi, by default when we convert Enum to String its return face value of Enum i.e. how it has written, is there any way we can get customized String values from Enum, I am thinking of method like toString, does Enum provides toString() in Java ?
Post a Comment