[HD-OCP17/21-Fundamentals Pg 303, Sec. 12.6.0 - switch-statement-and-expression-summary]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
radupana
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]

Post 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".

admin
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]

Post 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.

admin
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]

Post 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");
        };
    }

radupana
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]

Post 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
Last edited by radupana on Mon Sep 09, 2024 11:45 am, edited 1 time in total.

admin
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]

Post 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.

radupana
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]

Post 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)

admin
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]

Post 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.

radupana
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]

Post 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)

admin
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]

Post by admin »

Confirmed.
As per specification also, it should not work.

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests