Java 7 – New Switch Statement

Java SE 7 is still just around the corner. It is still in Beta mode, so many things are still under test. TechieDan has been reading and researching about Java 7 in order to keep abreast of what Java has to offer in the coming future.

Java 7

There is this Project CoinFish that the Java people has put up that there are loads and loads of contribution. As of Java 7, one of these features is the new way of using SWITCH statements.

I am sure most programmers would know that a switch statement is just like a if else statement. Though, the switch statement feels cleaner in terms of coding practice.


public static void getLove() {
  int test = 1;
  switch(test) {
    case 1: System.out.println("This is number 1"); break;
    default: System.out.println("Chose no love");
  }
}

The above is a sample switch statement if the method getLove() is called, it will print “This is number 1”.

Of course, one thing that many people still uses the if statement is because it is able to compare with Strings.

If (StringA.equals(StringB))
  System.out.println("It’s the same");

Now fast forward to Java SE 7, you can now use Strings inside a switch statement. TechieDan will now explore this possibility with you and how it has changed in Java SE 7 onwards. It’s still in beta so you can wait till it’s finalized.

Here I have a full complete source code so that you can run on your java source code editor and try to run it as an application.

public class SwitchJava7 {
  public static int getAge(String name) {

    int expectedAge = 0;

    if (name == null) { return expectedAge; }

    switch (name.toLowerCase()) {
      case "takeshi": expectedAge = 17; break;
      case "justin": expectedAge = 18; break;
      case "tony": expectedAge = 19; break;
      case "techiedan": expectedAge = 20; break;
      case "apek": expectedAge = 25; break;
      case "timorthy": expectedAge = 26; break;
      case "richard": expectedAge = 27; break;
      case "james": expectedAge = 28; break;
      case "max": expectedAge = 35; break;
      case "jason":  expectedAge = 36; break;
      case "david": expectedAge = 37; break;
      case "darren": expectedAge = 40; break;
      default: expectedAge =  0; break;
    }

    return expectedAge;
  }

  public static void main(String[] args) {

    String name = "TechieDan";
    int expectedAge = Java7Switch.getAge(name);

    if (expectedAge == 0) {
      System.out.println("You must be a Bieber fan. Strange?");
    } else {
      System.out.println("This is your age: " + expectedAge);
    }
  }
}

Forgive me on the Bieber pun intended but I definitely am not a fan of Bieber. Running this will yield this result.

This is your age: 20

Previous versions of Java while compiling this will produce an error below.

Cannot switch on a value of type String. Only convertible int values or enum constants are permitted.

Java 7 makes it flexible and the above is an example of how it is similar to String.equals() method. Java 7 promises more enhancement and this is where we all learn the language together.

Until next time, TechieDan would cover a few more enhancements on Java SE 7.

One Response

  1. danielctw March 7, 2011

Leave a Reply