About Question enthuware.ocajp.i.v7.2.1045 :

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

Moderator: admin

Post Reply
vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

About Question enthuware.ocajp.i.v7.2.1045 :

Post by vchhang »

I can't seem to find anywhere in the JLS that states the default statement must be the last label.

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

Re: About Question enthuware.ocajp.i.v7.2.1045 :

Post by admin »

vchhang wrote:I can't seem to find anywhere in the JLS that states the default statement must be the last label.
Because it can be anywhere. That is why it is not a correct option.
If you like our products and services, please help us by posting your review here.

yegomosc
Posts: 1
Joined: Thu Feb 11, 2016 2:49 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1045 :

Post by yegomosc »

Hi,

Code: Select all

 public static void main(String[] args) {
        char a = 'a';
        byte b = (byte) a;
    }
the above code piece of code had to contains expliciite casting to byte

but in the Line //1 it was not required (Why?? :shock: )

Code: Select all

void test(byte x) {
        switch (x) {
            case 'a':   // 1
            case 256:   // 2
            case 0:     // 3
            default:   // 4
            case 80:    // 5
        }
    }
I couldn't find the explanation, why it is possible that it worked?

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

Re: About Question enthuware.ocajp.i.v7.2.1045 :

Post by admin »

In Java, char data type is actually a numeric type that can have any value between 0 to 2^16-1. It is interpreted by the JVM as a unicode character. Therefore, 'a' is actually nothing but same as its ascii value 96.

Now, since 96 can perfectly fit within a byte, the case statement above works. But your next case 256 will not compile because 256 will not fit in a byte. (A byte can store numbers from -128 to 127 only).
If you like our products and services, please help us by posting your review here.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1045 :

Post by NickWoodward »

don't mean to butt in, but i think he or she is asking why the variable char a needs a cast, but 'a' does not.

pretty sure it's because the compiler knows what 'a' is at compile time, it's a literal. it knows that 'a' fits in a byte.

char a = 'a';
byte b = (byte) a;

however char a could've been changed for all the compiler knows - the compiler doesn't run code - so it cannot be sure that char a can still fit into the scope or range of a byte.

i'm sure someone else can explain it better than i have, but i think that's the general idea.

nick

likejudo
Posts: 17
Joined: Sun Feb 18, 2024 7:21 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1045 :

Post by likejudo »

When I try to cast it to a byte, I get error

java: duplicate case label


I don't understand why.

Code: Select all

    void test(byte x){
        switch(x){
            case 'a':   // 1
            case (byte) 256:   // 2
            case 0:     // 3
            default :   // 4
            case 80:    // 5
        }
    }

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

Re: About Question enthuware.ocajp.i.v7.2.1045 :

Post by admin »

(byte) 256 is 0.
If you like our products and services, please help us by posting your review here.

likejudo
Posts: 17
Joined: Sun Feb 18, 2024 7:21 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1045 :

Post by likejudo »

thanks!

Code: Select all

        System.out.println((byte) 256);
0

Post Reply

Who is online

Users browsing this forum: No registered users and 85 guests