3

Possible Duplicate:
Why can’t your switch statement data type be long, Java?

Why is something like that not possible?

private void importantFunction( long arg1 ) {

    switch (arg1){
    case 0:
         // do stuff
        break;
    case 1:
         // do other stuff
        break;
    default:
        //do default stuff
        break;
    }
}

The way I understand long values, there shouldn't be that much of a difference between them and integers.

Community
  • 1
  • 1
Robert Schröder
  • 169
  • 2
  • 11
  • 9
    http://stackoverflow.com/questions/2676210/why-cant-your-switch-statement-data-type-be-long-java – bartlaarhoven Dec 19 '12 at 11:30
  • "Why is something like that not possible?" Its one of the things I like about the Java language, its designed to prevent you from introducing design flaws as far as is possible. If you are switching over a long value, you are doing something wrong. – Gimby Dec 19 '12 at 13:34
  • Well it may be a bad decision if I would switching between a ton of values. But in my case I'm working with a proprietary api, which gives me a long value. I'm just checking if the value is 0, 1 or not set yet and a simple cast solves this in an easy way, but I was curious why Java does not allow it. A bad design with integer values is possible without any difficulties. The class Integer supports 2*2^31-1 different values. – Robert Schröder Dec 19 '12 at 14:34

4 Answers4

5

As per Java 7 you could use a String: LINK TO ORACLE

So basicly this would allow String.valueOf(23423423L);

IDKFA
  • 436
  • 3
  • 5
5

There are many, many features Java could have had. Language design is a continuous series of trade-offs between utility and complexity. Put in too many features, and the language becomes hard to learn and implement.

Obviously, one can have a long that has only a few possible values, so that it is reasonable to use it as a switch expression, but that is unusual. Mainly, long is used when there are too many values for int, and so far too many values for a switch expression.

One solution is a Map that maps the values of the long you would like to use to a small set of Integer values:

import java.util.HashMap;
import java.util.Map;

public class Test {
  public static void main(String[] args) {
    final int ONE_CASE = 3;
    final int ANOTHER_CASE = 4;
    Map<Long, Integer> map = new HashMap<Long, Integer>();
    map.put((long) 1e10, ONE_CASE);
    map.put((long) 1e11, ANOTHER_CASE);
    long arg1 = (long) 1e11;
    switch (map.get(arg1)) {
    case 3:
      System.out.println("ONE_CASE");
      break;
    case 4:
      System.out.println("ANOTHER_CASE");
      break;
    }
  }
}
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
3

JVM spec says: The Java virtual machine's tableswitch and lookupswitch instructions operate only on int data.

Community
  • 1
  • 1
Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
0

because Java doesn't want you to switch over those many options as a long can contain, so they restricted it to ints

vishal_aim
  • 7,636
  • 1
  • 20
  • 23