About Question enthuware.ocajp.i.v8.2.908 :

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

Moderator: admin

Post Reply
rezebric
Posts: 1
Joined: Fri Jan 01, 2016 5:49 am
Contact:

About Question enthuware.ocajp.i.v8.2.908 :

Post by rezebric »

The correct answer is "It will not compile." not the "Exception at run time."

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.908 :

Post by admin »

I see that "It will not compile" is indeed marked as the correct answer. Please let me know if you see something else.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

dev_khaled
Posts: 1
Joined: Sat Aug 21, 2021 12:21 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.908 :

Post by dev_khaled »

Hi, thank you for the great work. There is a bit misleading term in the explanation:
Item 4. All case labels should be COMPILE TIME CONSTANTS. You cannot have variables as case labels.
The highlighted sentence is a bit misleading. As you can have final variables with values evaluated at compile time as a case labels.

Try the following code

Code: Select all

public class Main {
    public static void main(String args[]) {
        
        int full = Integer.parseInt("20");
        
        
        // Next line causes compilation error because half's value cannot be evaluated at compile time!
        // final int half = full/2;
        
        
        // Next line causes compilation error because half is not final!
        // int half = 10;
        
        
        // Next line works perfectly! A final variable with value evaluated at compile time!
        final int half = 5 * 2;
        
        
        switch(full/2){
            case half: System.out.println("Final variable can work with case label!");
        }
    }
}
or run it directly from https://www.online-java.com/p1Bs95m8Yt

Suggestion: add clarification to the 2nd sentence

4. All case labels should be COMPILE TIME CONSTANTS. By either being a Literal value. eg: 50 or a Final variable whose value is evaluated at compile time. eg: final int someLabel = 5 * 2

Thank you.
Last edited by dev_khaled on Sat Aug 21, 2021 6:38 am, edited 1 time in total.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.908 :

Post by admin »

You are right.
Explanation has now been enhanced.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 42 guests