Page 1 of 1
[HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
Posted: Mon Sep 09, 2024 7:17 am
by radupana
Hi all,
This is related to Hanumant Deshmukh's great book on the OCP 17/21 exam, dated Saturday 10th August, 2024 Build 2.1.
On page 337, section 12.6 Switch statement and expression Summary, the row for Selector expression type mentions that the selectors are "Limited to: byte, short, int, char, and long and their wrappers, String, and enums
Not Allowed: boolean, float, and double"
However, long/Long are NOT allowed as a selector; compilation would fail with an error message saying: "Selector type of 'long' is not supported".
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
Posted: Mon Sep 09, 2024 10:27 am
by admin
You are right. Although it notes this correctly in section 12.3 , "Prior to Java 21, a the switch’s selector expression was restricted to only a few types but with Java 21 the only restriction that still remains is that a selector expression must not return long, float, double, or boolean. "
But miscategorizes long in the summary. Should be fixed.
Please mention your name so that we can credit you in the book for reporting this issue.
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
Posted: Mon Sep 09, 2024 10:37 am
by admin
Sorry, spoke too soon. long is allowed in Java 21. It is not allowed in Java 17. So the book is correct but it should note the difference in the versions.
This works in Java 21:
Code: Select all
public static void main(String[] args) {
long l = 10L;
int y = switch(l){
default -> 20;
};
switch(l){
default -> System.out.println("long");
};
}
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
Posted: Mon Sep 09, 2024 10:49 am
by radupana
Hmmm, odd.
I am using Java 21 and am getting the error mentioned above:
% javac Switch.java
Switch.java:42: error: selector type long is not allowed
switch (l) {
^
1 error
% java -version
openjdk version "21.0.3" 2024-04-16 LTS
OpenJDK Runtime Environment Temurin-21.0.3+9 (build 21.0.3+9-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.3+9 (build 21.0.3+9-LTS, mixed mode)
The Java language spec (
https://docs.oracle.com/javase/specs/jls/se21/jls21.pdf) mentions, in section 14.11: The Expression is called the selector expression. The type of the selector expression
must be char, byte, short, int, or a reference type, or a compile-time error occurs.
And in section 15.28: The Expression is called the selector expression. The type of the selector expression
must be char, byte, short, int, or a reference type, or a compile-time error occurs
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
Posted: Mon Sep 09, 2024 10:51 am
by admin
That is indeed weird. I just ran this code on Java 21 and it works fine:
Code: Select all
public class TestClass{
public static void main(String[] args) {
long l = 10L;
int y = switch(l){
default -> 20;
};
switch(l){
default -> System.out.println("long");
};
}
}
May be in Java 21, long is boxed into a Long and the switch now becomes a switch with pattern matching (which allows all reference types).
Or it could be a bug in Java 21 implementation. I see you tried it on Java 22. So may be they fixed it. Need to investigate more.
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
Posted: Mon Sep 09, 2024 11:01 am
by radupana
I can confirm it does not compile on:
openjdk version "21.0.3" 2024-04-16 LTS
OpenJDK Runtime Environment Temurin-21.0.3+9 (build 21.0.3+9-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.3+9 (build 21.0.3+9-LTS, mixed mode)
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
Posted: Mon Sep 09, 2024 11:03 am
by admin
Ok, mine is:
java version "21" 2023-09-19 LTS
Java(TM) SE Runtime Environment (build 21+35-LTS-2513)
Java HotSpot(TM) 64-Bit Server VM (build 21+35-LTS-2513, mixed mode, sharing)
May be they fixed in the later build.
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
Posted: Mon Sep 09, 2024 11:44 am
by radupana
Interesting.
Can confirm it fails on 22 as well:
openjdk version "22.0.2" 2024-07-16
OpenJDK Runtime Environment (build 22.0.2+9-70)
OpenJDK 64-Bit Server VM (build 22.0.2+9-70, mixed mode, sharing)
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
Posted: Mon Sep 09, 2024 7:18 pm
by admin
Confirmed.
As per specification also, it should not work.