0

I have noticed that I cannot use long as expression of the switch() part.

I don't understand why.

This primitive worse than others?

What's wrong with long?

Kara
  • 6,115
  • 16
  • 50
  • 57
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

5 Answers5

1

It's how switch was built into Java. What you can do, though, is cast long values as int types before you put them into switch.

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
1

From the Oracle Documentation:

A switch works with the byte, short, char, and int primitive data types.

The reason you can't switch on a long is that there is no need. In order to meaningfully switch on a long, you would need to add a case statement for every single value you want to switch to. (The only reason ints are supported is because that is the default integral type; even for ints its rather a waste.)

AJMansfield
  • 4,039
  • 3
  • 29
  • 50
1

You can use a Map as a sort of switch

Eg instead of:

long choice = getChoice();
if (choice == 1L) {
   System.out.println("Do action one");
} else if (choice == 2L) {
   System.out.println("Do action two");
} else {
   System.out.println("Default action");
}

you can do

Map<Long, Runnable> actionMap = new HashMap<>();
actionMap.put(1L, () -> System.out.println("Do action one"));
actionMap.put(2L, () -> System.out.println("Do action two"));
Runnable defaultAction = () -> System.out.println("Default action");
...
Long choice = getChoice();
Runnable action = actionMap.getOrDefault(choice, defaultAction);
action.run();

The map approach will execute in constant time O(1) whereas the if/then/else will execute in linear time O(N)

lance-java
  • 25,497
  • 4
  • 59
  • 101
0

As other people have said, you can't use long (or String etc) in a case statement. If you really want to use a long, the obvious choice is to use if/then/else.

eg:

if (someValue == 1L) { 
    println("one"); 
} else if (someValue == 2L) { 
    println("two"); 
} ...
} else if (someValue == 1000L) { 
    println("thousand"); 
}

But if you have many options, this will perform very badly since you need to evaluate each condition. What I was suggesting in my other answer is to use a Map instead

Map<Long, Runnable> map = new HashMap<Long, Runnable>();
map.put(1L, new Runnable() {
    public void run() { println("one"); }
});
map.put(2L, new Runnable() { ... });
...
map.put(1000L, new Runnable() { ... });

And then you can use

map.get(someValue).run();

The map version will perform in constant time O(1) whereas the if/then/else will perform in linear time O(N). As you can see, this construct can be used with Long, String or any object you like.

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • Since Long is not allowed in the Java language for a switch statement, tell us what you are trying to do and perhaps we can offer an alternative. – ErstwhileIII Mar 03 '14 at 16:25
  • I'm creating a switch construct based on long which performs in constant time FFS!!! – lance-java Mar 03 '14 at 16:26
  • You will need to transform your "Long" value into an integer ... is there a reasonable mapping to do so? If you have a set of descrete Long values that are used, then one alternative is to create a HashMap map, and use the "int" in the switch statement. – ErstwhileIII Mar 03 '14 at 16:29
  • I give up... I can't be arsed trying to explain myself any more – lance-java Mar 03 '14 at 16:30
-1

When you use a programming language, you are restricted to that language's definition. In this case, the Java switch statement

works with the byte, short, char, and int primitive data types

according to the Java language specification.

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
  • Sure would like to understand the downvote for a accurate answer! – ErstwhileIII Mar 03 '14 at 16:24
  • because your answer is not helpful. your answer is not response to my question. – gstackoverflow Mar 03 '14 at 16:29
  • I can understand your frustration .. but you are closer to what you are trying to do than we are. Can you give a snippet of your code so we can try to help? – ErstwhileIII Mar 03 '14 at 16:39
  • I have not code. it is question from certification – gstackoverflow Mar 03 '14 at 16:42
  • If that is a question from a certification test, then the simple answer is that the Java language specification does not allow a long type in the switch statement. There is nothing wrong with Java. There is nothing inherently wrong with a long. The language only allows int and certain other primitive types that can be converted into an int to be used in a switch statement. A long cannot be converted to an int without losing information. – ErstwhileIII Mar 03 '14 at 16:49