Java Program to Convert Enum to String
Last Updated :
17 Jun, 2021
Given an enum containing a group of constants, the task is to convert the enum to a String.
Methods:
We can solve this problem using two methods:
- Using name() Method
- Using toString() Method
Let us discuss both of them in detail and implementing them to get a better understanding of the same.
Method 1: Using name() Method
It returns the name of the enum constant same as declared in its enum declaration.
- We would be using name() method to return the name of the enum constant.
- In the main class, we just have to print it.
- The value given inside is first the name of the enum class that we will create further, then calling the constant that is named, finally using the name() method.
- Now create another java enum file named Fruits.java in the same folder where you created the main file, and declare the enum as follows:
Example
public enum Fruits {
Orange, Apple, Banana, Mango;
}
Java
// Java Program to Convert Enum to String
// using
// Importing input output classes
import java.io.*;
// Enum
enum Fruits {
Orange,
Apple,
Banana,
Mango;
}
// Main class
class GFG {
// Main driver method
public static void main(String[] args) {
// Printing all the values
System.out.println(Fruits.Orange.name());
System.out.println(Fruits.Apple.name());
System.out.println(Fruits.Banana.name());
System.out.println(Fruits.Mango.name());
}
}
OutputOrange
Apple
Banana
Mango
Method 2: Using toString() Method
It is used to get a string object which represents the value of the number object.
- We will be following the same procedure as earlier used but the only difference here is that we will be using toString() method. So just replace name() method with toString() method.
Note: Do not forgot to create a Fruits.java enum file in the same folder.
Illustration:
public enum Fruits {
Orange, Apple, Banana, Mango;
}
Example 2
Java
// Java Program to Convert Enum to String
// Using toString() Method
// Importing input output classes
import java.io.*;
// Enum
enum Fruits {
// Custom entries
Orange,
Apple,
Banana,
Mango;
}
// Main class
class Main {
// Main driver method
public static void main (String[] args) {
// Printing all the values
System.out.println(Fruits.Orange.toString());
System.out.println(Fruits.Apple.toString());
System.out.println(Fruits.Banana.toString());
System.out.println(Fruits.Mango.toString());
}
}
OutputOrange
Apple
Banana
Mango
Similar Reads
Java Program to Convert Long to String The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String. Given a Long number, the task is to convert it into a
4 min read
Java Program to Convert String to Object In-built Object class is the parent class of all the classes i.e each class is internally a child class of the Object class. So we can directly assign a string to an object. Basically, there are two methods to convert String to Object. Below is the conversion of string to object using both of the me
2 min read
Java Program to Convert Boolean to String In Java programming, you might come across the need to convert a simple "true" or "false" boolean value into a String. It may seem like a challenging task, but fear not! In this article, You will explore some methods to convert a boolean value to a string in Java Method for Boolean to String Convers
4 min read
Java Program to Convert OutputStream to String OutputStream is an abstract class that is available in the java.io package. As it is an abstract class in order to use its functionality we can use its subclasses. Some subclasses are FileOutputStream, ByteArrayOutputStream, ObjectOutputStream etc. And a String is nothing but a sequence of character
2 min read
Convert String to Stream of Chars in Java The StringReader class from the java.io package in Java can be used to convert a String to a character stream. When you need to read characters from a string as though it were an input stream, the StringReader class can be helpful in creating a character stream from a string. In this article, we wil
2 min read