Saturday, November 23, 2019

How To Convert Enum To String Inwards Coffee Alongside Example

Enum to String Conversion Example inward Java
There are 2 ways to convert an Enum to String inward Java, commencement yesteryear using name() method of Enum which is implicit method in addition to available to all Enum in addition to minute yesteryear using toString() method. name() method of Enum returns exact same String which is used to declare a detail Enum instance similar inward WeekDays Enum if nosotros convey MONDAY every bit 1 Enum instance than the name() will render String "MONDAY". This method of conversion from Enum to String is useful if String representation of Enum is same every bit its String mention but if yous convey dissimilar String representation in addition to then yous tin purpose toString() method. Since Enum inward Java allows a programmer to override an inherited method in addition to since Enum has access to all Object cast method, yous tin easily override toString() method to render a custom String implementation for an Enum instance which tin farther purpose to convert that Enum instance to String inward Java.

Both ways of converting Enum to String has in that place ain merit in addition to purpose cases. Prefer name() method if String representation of Enum instance is its declared mention in addition to prefer toString() method if every Enum Instance has in that place ain custom String representation.

For example, if declared enum is RED in addition to yous desire text representation every bit "RED" in addition to then purpose name() method because both are same, but if yous desire String representation every bit "Red" in addition to then yous tin purpose toString() method to customize that bit.

Its truly amend to use toString() from maintenance perspective because if whatsoever developer refactor in addition to alter the mention of Enum constant in addition to then its String representation volition too change, which is non a desirable coupling in addition to should last avoided at all cost. 



Java Example to Convert Enum to String
In next illustration nosotros volition run into both agency of converting Enum into String inward Java. This programme too demonstrate purpose of Enum.values() method which returns all Enum instances, which tin last purpose to iterate over all enum constants. You tin only iterate or impress values or create anything yous would similar to amongst those constants.

Enum to String Conversion Example inward Java How to convert Enum to String inward Java amongst Example
In this example, yous tin run into nosotros convey 2 enums Weekdays in addition to ColdDrink, commencement uses name() method for converting Enum constant to String acre minute uses toString() method instead.

package example;
 
/**
 * Java programme to demonstrate how to convert Enum to String inward Java. This programme demonstrate
 * 2 examples yesteryear using name() in addition to toString() method to acquire a meaningful String representation
 * from an Enum instance inward Java.
 *
 * @author Javin Paul
 */

public class EnumToString {
    private enum Weekdays {
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
    } 


    private enum ColdDrink {
        PEPSI("Pepsi"), COKE("Coca Cola"), SPRITE("Sprite");
        private String brandname;
        private ColdDrink(String brand) {
            this.brandname = brand;
        }
       
        @Override
        public String toString(){
            return brandname;
        }
    }
   
   
    public static void main(String args[]) {
        //Converting Enum to String yesteryear using name() method
        //by default impress mehtod calls toString() of enum
        ColdDrink[] drinks = ColdDrink.values();
        for (ColdDrink potable : drinks) {
            System.out.printf("String to Enum illustration using mention :  %s%n", drink.name());
        }

        //Converting Enum to String using toString() method
        for (ColdDrink potable : drinks) {
            System.out.println("String to enum conversion using toString() : " + drink);
        }
    }
}
Output:
String to Enum illustration using mention :  PEPSI
String to Enum illustration using mention :  COKE
String to Enum illustration using mention :  SPRITE
String to enum conversion using toString() : Pepsi
String to enum conversion using toString() : Coca Cola
String to enum conversion using toString() : Sprite

That’s all on How to convert Enum into String inward Java. Enum provides dissimilar ways to render meaningful String representation amongst utmost flexibility offered yesteryear allowing overriding toString method inward Java. For most cases name() method would last plenty to convert Enum to String.

If yous desire to read farther most Enum, banking concern correspond out this amazing Java Enum Tutorials from Blog :
  1. How to purpose valueOf method of Enum (check here)
  2. How to loop over Enum constants inward Java (example)
  3. Top xv Java Enum Interview Questions amongst Answers (see here)
  4. Can nosotros purpose Enum inward Switch Statement inward Java (Yes)
  5. Java tip to convert  Enum to String inward Java (see tip)
  6. String to Enum inward Java amongst Example (check here)
  7. Difference betwixt RegularEnumSet in addition to JumboEnumSet inward Java (read here)
  8. What Every Java Programmer Should know most Enum (click here)
  9. 10 points most Enum inward Java (see here)
  10. Learn how to purpose Enum inward Java amongst a Simple Example (check here)
  11. Can Enum convey Constructor inward Java (learn here)

No comments:

Post a Comment