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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

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: 10384
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.

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: 10384
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!

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests