Java Tutorial 15. Switch statements
Loading...
3,912
Loading...
Uploader Comments (Zantorc)
see all
All Comments (2)
-
check out my channel for java programming tutorials!!!
-
Hey. I was wondering if you could use the '>' or '=>' operators etc. in the cases?
Loading...
(First '=>' isn't an operator so I assume you mean '>='). You can use these operators but only in a rather contrived way. The word 'case' has to be followed by a constant known at compile time. Also it has to be assignable to the type of the expression which follows the word 'switch'. That expression in turn must be of type byte, Byte, short, Short, char, Character, int, Integer or an enum type. NOT boolean.
Zantorc 3 years ago
I had an example which illustrates this but after over an hour of trying I've discovered youtube blocks anything which doesn't look like plain text.
Basically whatever you put after 'case' has got to be known at compile time. It could be a ternary expression involving conditional operators, which looks a bit odd, but it works. The important thing is the compiler can work out what it is (so you might as well have put that value in to begin with).
Zantorc 3 years ago