[HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
Moderator: admin
-
- Posts: 28
- Joined: Fri Feb 09, 2018 11:50 am
- Contact:
[HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
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".
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".
-
- Site Admin
- Posts: 10385
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
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.
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.
-
- Site Admin
- Posts: 10385
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
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:
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");
};
}
-
- Posts: 28
- Joined: Fri Feb 09, 2018 11:50 am
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
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
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
Last edited by radupana on Mon Sep 09, 2024 11:45 am, edited 1 time in total.
-
- Site Admin
- Posts: 10385
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
That is indeed weird. I just ran this code on Java 21 and it works fine:
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.
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");
};
}
}
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.
-
- Posts: 28
- Joined: Fri Feb 09, 2018 11:50 am
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
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)
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)
-
- Site Admin
- Posts: 10385
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
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.
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.
-
- Posts: 28
- Joined: Fri Feb 09, 2018 11:50 am
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
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)
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)
-
- Site Admin
- Posts: 10385
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]
Confirmed.
As per specification also, it should not work.
As per specification also, it should not work.
Who is online
Users browsing this forum: Google [Bot] and 8 guests