Skip to content

Commit 41c8edd

Browse files
committed
Update switch
1 parent 3b20e09 commit 41c8edd

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

src/SUMMARY.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,38 @@
1212
- [Tour of Scala](./examples/tour_of_scala.md)
1313
- [tutorialspoint](./examples/tutorialspoint.md)
1414
- [tutorialspoint paid course](./examples/tutorialspoint_paid.md)
15-
- [w3schools](./examples/w3schools.md) -->
15+
- [w3schools](./examples/w3schools.md)
16+
17+
Project ideas:
18+
calorie tracker
19+
tic tac toe
20+
chess
21+
go
22+
CSV
23+
image stuff with the PPM format
24+
25+
swing game - maybe have a simplified game engine
26+
27+
fixed time loop game
28+
29+
opengl/ffm
30+
31+
version control
32+
33+
something with statistics
34+
newton raphston
35+
36+
airplane physics program
37+
ball throw physics program
38+
chemical stuff
39+
40+
(after bytes) Make an audio file. Play hot cross buns.
41+
42+
snake
43+
2048
44+
colors
45+
battleship
46+
-->
1647

1748
# Modern Java
1849

src/switch/exhaustiveness.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,36 @@ String describe(int number) {
2323
}
2424
```
2525

26-
When you have something like an enum you don't need a `default` case
27-
because you can handle every variant explicitly.
26+
When you have something like an enum you might think you don't need a `default` case
27+
because you can handle every variant explicitly.[^sometimes]
2828

29-
```java,no_run
29+
```java,no_run,does_not_compile
30+
enum Bird {
31+
TURKEY,
32+
EAGLE,
33+
WOODPECKER
34+
}
35+
36+
boolean isScary(Bird bird) {
37+
switch (bird) {
38+
case TURKEY -> {
39+
return true;
40+
}
41+
case EAGLE -> {
42+
return true;
43+
}
44+
case WOODPECKER -> {
45+
return false;
46+
}
47+
}
48+
}
49+
```
50+
51+
This is, unfortunately, not the case for a switch statement.
52+
You either need a `default` case or to have an explicit `case null` to handle the
53+
possibility that the enum value is `null`.[^lies]
54+
55+
```java,no_run,does_not_compile
3056
enum Bird {
3157
TURKEY,
3258
EAGLE,
@@ -44,6 +70,17 @@ boolean isScary(Bird bird) {
4470
case WOODPECKER -> {
4571
return false;
4672
}
73+
// Need to handle the possibility of null
74+
// or give a "default ->"
75+
case null -> {
76+
// You might want to return a value or just crash
77+
return false;
78+
}
4779
}
4880
}
49-
```
81+
```
82+
83+
[^sometimes]: This is sometimes the case! It is really just this specific form of switch that has this restriction.
84+
85+
[^lies]: Remember at the very start when I said I was going to lie to you? This is one of those lies. The real reason for this restriction has to do with how Java compiles switch statements and a concept called "separate compilation." Basically
86+
even though we know you covered all the enum variants, Java still makes you account for if a new enum variant was added later on. It doesn't do this for all forms of `switch` though.

0 commit comments

Comments
 (0)