Switch | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Switch

Is it possible for a switch case to work if we don't initialise the variable to 0?

13th Feb 2018, 6:06 AM
Geetali
Geetali - avatar
3 Answers
+ 10
Whenever in doubt, just try it out on you own as you can always code it in the Code Playground straightaway. Then you'll ask if you couldn't figure out why it works or not working. 😉
13th Feb 2018, 6:45 AM
Zephyr Koo
Zephyr Koo - avatar
+ 1
Nope, if the variabile don't take any value during the execution of the code before the Switch, the statment Will receive a null element giving you an happy NullPointerException, so java want the variabile to be initialised
13th Feb 2018, 6:26 AM
Leonardo Medai Cossutta
Leonardo Medai Cossutta - avatar
+ 1
Thnx
13th Feb 2018, 6:41 AM
Geetali
Geetali - avatar