• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Why did I use word 'default' in switch for java?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am learning switch statements. I write some codes like this:


I was asked to add "case" or "default" or '}' after I compiled my code. I tried to change code "case 2: " to "default:", it worked. I was told that the compiler would check the type of all case options without the actual values. Is it right? Should I add 'defaultl' always?
 
Sheriff
Posts: 17331
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
default is an optional clause for the switch statement so, no, you don't need to always add it.

Think of the default clause as the last "else" of a series of if-else-if statements. That means it catches "everything else that wasn't caught by the previous conditions". If you don't provide it, then nothing will happen when none of the conditions match. In your case, for example, nothing happens if computerMove is 3 or 4.

And Welcome to the Ranch!
 
Junilu Lacar
Sheriff
Posts: 17331
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More specifically, if your switch statement has no default clause, computerMove could remain uninitialized. Since a local variable must be definitely assigned a value, the compiler will notice that and will report an error telling you that the System.out.println() statement could still be dealing with an uninitialized variable, which is not allowed.
 
Yang Hu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you mean that computerMove should initialize since it is a local variable?
 
Yang Hu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the java compiler check the type of all options or actual values?
 
Junilu Lacar
Sheriff
Posts: 17331
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yang Hu wrote:Did you mean that computerMove should initialize since it is a local variable?


Either that or you provide a default clause to set computerMove to something, even if it's just null.
 
Junilu Lacar
Sheriff
Posts: 17331
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yang Hu wrote:Does the java compiler check the type of all options or actual values?



I believe all case labels have to be constant expressions or enum constants. The type of each case label must be "assignment compatible" with the type of the switch expression.
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . I believe all case labels have to be constant expressions or enum constants. . . .

That is correct, as you can see from the Java Language Specification (=JLS).
 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic