• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to use Enum in Switch?

February 19, 2009 //  by Krishna Srinivasan//  Leave a Comment

Enums introduced in Java 5.0 (Read: New features in Java 5.0) allows switching to be done based on the enums values. It is one of the greatest new features introduced from the version 5.0. Prior to Java 1.4, switch only worked with int, short, char, and byte values. However, since enums have a finite set of values, Java 1.5 adds switch support for the enum. Some of the advantages of using enums are it is strongly typed, Comparing enums is faster, you can use == instead equals() and Enums can implement interfaces, methods and functions.

also read:

  • Autoboxing in Java 5.0
  • Annotations in Java 5.0
  • Generics in Java 5.0
  • Java books

Here’s an example of using an enum in a switch statement.

public class EnumSwitcher {
public enum Grade { A, B, C, D, F, INCOMPLETE };

public static void main(String[] args) {
Grade examGrade	= Grade.C;

System.out.println('Switching based on the enums values.');
System.out.println();
switch (examGrade) {
	case A:
		System.out.println('The students grade is ' + Grade.A);
		break;
	case B: // fall through to C
	case C:
		System.out.println('The students grade is ' + Grade.C);
		break;
	case D: // fall through to F
	case F:
		System.out.println('The students grade is ' + Grade.F);
		break;
	case INCOMPLETE:
		System.out.println('The students grade is ' + Grade.INCOMPLETE);
		break;
	default:
		System.out.println('The students grade is unknown.');
		break;
}

}
}

Given below is the output of the above program.

Switching based on the enums values.

The students grade is C

As you can see from the above example, there is nothing special in using an enum with a switch case statement.

Category: JavaTag: Enum, Java 5.0, switch

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Adding methods to an Enum
Next Post: Working with arrays : java.util.Arrays class »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

EJB 3.0 Timer Services

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact