Java Enum String Conversion
Enums in Java are more than just constants—they are objects with methods. Often, you’ll need to convert enums to strings (for logging, displaying, or saving to a database) or convert strings back to enums (when reading user input or config files).
Java provides built-in methods to handle this:
- name()
- toString()
- valueOf()
In this guide, we’ll cover:
- How to convert enums to strings
- How to parse strings into enums
- Differences between name() and toString()
- Best practices
Converting Enum to String
1. Using name()
- Returns the exact name of the enum constant as defined.
- Always returns in uppercase (unless you named it differently).
public class EnumNameExample {
enum Level { LOW, MEDIUM, HIGH }
public static void main(String[] args) {
Level l = Level.HIGH;
System.out.println(l.name()); // HIGH
}
}
2. Using toString()
- By default, behaves the same as name().
- Can be overridden to provide a custom string.
public enum Day {
MONDAY(“Start of Week”),
FRIDAY(“Almost Weekend”);
private String label;
Day(String label) { this.label = label; }
@Override
public String toString() {
return label;
}
}
public class EnumToStringExample {
public static void main(String[] args) {
System.out.println(Day.MONDAY); // Start of Week
System.out.println(Day.FRIDAY); // Almost Weekend
}
}
Converting String to Enum
1. Using valueOf()
- Converts a string to an enum constant.
- The string must match exactly (case-sensitive).
public class EnumValueOfExample {
enum Direction { NORTH, SOUTH, EAST, WEST }
public static void main(String[] args) {
Direction d = Direction.valueOf(“EAST”);
System.out.println(d); // EAST
}
}
If the string doesn’t match, it throws IllegalArgumentException.
2. Custom Parsing (Safer)
For user input, create a custom method to handle invalid strings gracefully.
public enum Level {
LOW, MEDIUM, HIGH;
public static Level fromString(String str) {
for (Level l : Level.values()) {
if (l.name().equalsIgnoreCase(str)) {
return l;
}
}
throw new IllegalArgumentException(“Invalid Level: ” + str);
}
}
public class EnumParseExample {
public static void main(String[] args) {
Level l = Level.fromString(“medium”); // works (case-insensitive)
System.out.println(l); // MEDIUM
}
}
Enum Utility Methods
- values() → returns an array of all constants.
- ordinal() → returns index (position) of the enum constant.
public class EnumUtilityExample {
enum Size { SMALL, MEDIUM, LARGE }
public static void main(String[] args) {
for (Size s : Size.values()) {
System.out.println(s + ” at index ” + s.ordinal());
}
}
}
Best Practices
- Use name() for debugging/logging (guaranteed exact constant name).
- Override toString() for user-friendly output.
- Use valueOf() cautiously—wrap it in try-catch or use custom parsing.
- Avoid relying on ordinal()—values may change if enum order changes.
Conclusion
Java provides multiple ways to convert enums to strings and back:
- name() → exact constant name (safe, fixed).
- toString() → customizable string representation.
- valueOf() → converts string back to enum (case-sensitive).
By combining these methods with custom parsing, you can handle enums in a way that’s both robust and user-friendly.