Monday 8 May 2017

Java Tutorial: Enum in java | Java enum [How to convert String to Enum - Operation]


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=4m1v7CnUesc&list=UUhwKlOVR041tngjerWxVccw

Operation.java
public enum Operation
{
    PLUS, MINUS, MULTIPLY, DIVIDE;

    long calculate(long x, long y)
    {
        switch (this)
        {
        case PLUS:
            return x + y;
        case MINUS:
            return x - y;
        case MULTIPLY:
            return x * y;
        case DIVIDE:
            return x / y;
        default:
            throw new AssertionError("Unknown operations " + this);
        }
    }

}
EnumDemo.java
public class EnumDemo
{
    public static void main(String[] args)
    {
        /*
         * Convert a String to Enum object.
         */
        Operation operation = Operation.valueOf("plus".toUpperCase());
        System.out.println(operation);
        
        System.out.println(operation.calculate(10, 20));
    }
}
Output
PLUS
30
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/EnumDemo_Convert_to_Enum_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/EnumDemo_Convert_to_Enum_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/45d5519a1474198166ac515ccd559cc996bb550c/BasicJava/EnumDemo_Convert_to_Enum_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • 1 comment:

    1. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.
      Java Training in Chennai

      ReplyDelete