Conversion from one data type to another datatype is required in most cases while coding. Different programming languages have several methods and functions for the conversion process. For example, the conversion of char to String, String to enum, and vice versa is implemented using different methods in Java, such as name(), toString(), etc. In this blog post, we will learn the conversions from the Enum to the Strings in Java.
Following is the table of contents in which we will proceed to grasp the conversion from enum to string in Java.
- What are Enum and strings in Java?
- Methods to Convert Enum to String
- Conversion of String back to Enum
- Conclusion
What are Enum and Strings in Java?
Enums in Java represent a set of constants that can not be changed. The Enum class implements the enum in Java. Examples of enums in Java are the days of the week, the compass directions, etc. While the strings are referred to as the chain of characters in Java. The strings are implemented using the string class of Java. The enums and stings in Java are immutable in nature which means that they can not be changed in the code after the declaration.
Now let us move towards the methods to convert the Enum to String in Java.
Methods to Convert Enum to String
In Java, the below-listed built-in methods are used for enum-to-string conversion:
- The name() Method
- The toString() Method
Let’s start with the name() method.
Method 1: The name() Method
This method in Java returns the name of the declared enum constant. It prints the enum constant in the form of strings in Java as depicted in the code below.
// Importing input-output packages
import java.io.*;
// Declare the Enum
enum Clothes {
EXTRASMALL, SMALL, MEDIUM, LARGE, EXTRALARGE;
}
// Main class
class ConvertExample1 {
// main() method
public static void main(String[] args) {
// Print all the values
System.out.println("The Conversion of Enum to String is:");
System.out.println(Clothes.EXTRASMALL.name());
System.out.println(Clothes.SMALL.name());
System.out.println(Clothes.MEDIUM.name());
System.out.println(Clothes.LARGE.name());
System.out.println(Clothes.EXTRALARGE.name());
}
}
In the above code block:
- The input-output package in Java is imported.
- The enum name “Clothes” is declared along with the values.
- In the next step, the class is created as “ConvertExample1” in Java.
- The strings are printed in the last part of the code.
Output
The strings are printed to the output console using the name() method.
Method 2: Using the toString() Method
The toString() method in Java returns the value in the string according to the input declared to it. The code below shows the conversion of the enum to Java using the toString() method.
// Importing input-output classes
import java.io.*;
// Declare the Enum
enum Directions {
EAST, WEST, NORTH, SOUTH;
}
// Main class
class ConvertExample2 {
// main() method
public static void main(String[] args) {
// Printing all the values
System.out.println("The Conversion of Enum to String is:");
System.out.println(Directions.EAST.toString());
System.out.println(Directions.WEST.toString());
System.out.println(Directions.NORTH.toString());
System.out.println(Directions.SOUTH.toString());
}
}
In the above block of code:
- The input-output package of Java is imported.
- The “Directions” is declared as an enum name and the values are assigned to it.
- The class is declared as “ConvertExample2” in the next step.
- The toString() method of the object class prints the strings according to the specified inputs.
Output
The output below shows that the values declared in the enums are successfully converted to strings.
Bonus Tip: Conversion of String To Enum in Java
The strings in Java can be converted back to the enum using multiple methods, as listed below.
- Using valueOf() Method
- Using Iteration
- Using Map in Java
- Using Stream API
We will discuss the valueOf() Method, which is the most simple way to convert the String to Enum in Java.
// Importing input-output classes
import java.io.*;
// Declare the Enum
enum Directions {
EAST, WEST, NORTH, SOUTH;
}
// Main class
class ConvertExample2 {
// main() method
public static void main(String[] args) {
String s = "EAST";
Directions dir = Directions.valueOf(s);
// Printing all the values
System.out.println("The Conversion of String to Enum is:" + dir);
}
}
In the above code block:
- The enum name is declared as “Directions” and the values are assigned to the enum.
- The String is declared as “s” which contains the enum.
- The conversion of string to enum takes place through the valueOf() method in the next step.
Output
The output below states the conversion of the declared string to enum.
This ends the discussion on different methods to convert the enum to a string in Java.
Conclusion
To convert an enum to a string in Java, a couple of built-in methods are used such as the name() method and the toString() method. To convert the string back to enum, the valueOf(), Map, and Stream API can be used. This Java blog has explained a practical demonstration of enum-to-string conversion. In addition to this, it has also elaborated on how to convert a string back to an enum in Java.