OCP 17 Exam - chapter 3 notes (switch, loops, pattern matching, flow scoping)
- “watch indentation and braces”, e.g. in the following example count is always incremented
1
2
3
if (count < 2)
System.out.println("Good day");
count++;
- “pattern matching was introduced to reduce boilerplate code, … like a lot of other new enhancements in Java”
- reassigning pattern variables is possible (can be prevented with
final)
1
2
if (count instanceof final Integer i)
i = 5; // DOES NOT COMPILE
- pattern variable must be subtype
1
2
3
Double d = 2.0;
if(d instance of Double)
if(d instance of Double data) // DOES NOT COMPILE, but compiles in JAVA 21
- flow scoping - “scope is determined by compiler, not hierarchy like class or local scoping”
1
2
3
4
5
private static flowScopingShowcase(Number num) {
if(!(number instance Integer integer))
return
System.out.println("it's integer " + integer.intValue());
}
- switch statement
case constantExpresion: { System.out.print("value"); }- break is optional. When
caseis matched and there will be nobreak;, then all subsequentcases will be evaluated as well. - default can be placed anywhere - doesn’t have to be at the end
- we can combine
casein two ways:case 1: case 2:orcase 1,2:(since Java 14) - supported types:
int,byte,short,char,String,enumandvar(if resolved to mentioned types) - if
casestatement is defined locally, then must befinal-
- switch expressions
case constantExpresion -> { yield "value"; }- assignment to variable is optional
defaultis only required if assigned to variable and not allcaseswere covered- if assigned to variable it must end with
; - block with
}cannot end with;(for switch statement it can!)
- loops
- we can use
varin for-each loop:for(var name : names) - statements after
break;DOES NOT COMPILE break LABEL;will go to expression stating withLABEL:- tip on exam is to SKIP too complex loops, because I won’t get extra points for solving this!
- we can use
Playground code
https://github.com/RG9/rg-playground-ocp17/blob/main/Chapter3.java
Credit: OCP Oracle Certified Professional Java SE 17 Developer Study Guide
This post is licensed under CC BY 4.0 by the author.