Clean Java

There is always a bigger game

String in switch-case statement in java 7

leave a comment »

One of the small enhancements added as part of java 7 project Coin is the support of String in the switch case statement. Pre java 7 world only characters and numbers were allowed in the switch case statement.

package org.sanju;

/**
 *
 * @author sanju.org
 *
 * String in switch-case statement in java.
 *
 */

public class StringInSwitchCase {

	public static void main(String args[]){
		new StringInSwitchCase().test("abc");
	}

	public void test(String str){

		switch(str){
		case "xyz":
			System.out.println("xyz");
			break;
		case "abc":
			System.out.println("abc");
			break;
			default:
			System.out.println("default");
		}
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: