About Question enthuware.ocajp.i.v7.-2-.1357 :

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

Moderator: admin

noeloo
Posts: 61
Joined: Sat Feb 15, 2020 8:56 am
Contact:

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

Post by noeloo »

1) above gparLondon posted following code:

Code: Select all

public static void main(String args[])
	{
		final byte b=10;
		char ch='c';
		byte cb='b';
		
		switch(ch)
		{
		case'a':System.out.println("a");break;
		case b:System.out.println("b");break;
		}
	}
and you wrote:
In your first code, case b:System.out.println("b");break; should not compile.
It was compiling for the gparLondon and is also compiling for me.
Could you give more details on why shouldn't it and help me figure out why I observe different behaviour?

2) another code from above:

Code: Select all

public class OverLoadingSample {

    public static void main(String args[])
    {
        call(10);
    }
    /*static void call(int i)
    {
        System.out.println("Int primitive");
    }*/
    static void call(short s)
    {
        System.out.println("short primitive");
    }
    static void call(byte b)
    {
        System.out.println("byte primitive");
    }

}
you wrote that it does not call byte/short methods, because "narrowing conversions are not allowed in method calls". But the discussion above was about this 10 - does it have any primitive type (int/short/byte) or not, and from what I understood, you claimed that it's not an int. And you prove it by referring to this JLS which tells exactly about narrowing ints - so for this to make sense you would have to agree that this 10 is in fact int, where am I wrong here?

3) I also tried the code from the question:

Code: Select all

        char ch = 'x';
        switch (ch) {
            case -1: System.out.println(-1); break;
            default: System.out.println("default");
        }
and the compiler does not say possible lost of precision, but incompatible types: possible lossy conversion from int to char - so it really looks like it's rather an int, I cannot understand that the code is an example for a byte but it doesn't even mention byte.

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

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

Post by admin »

Code: Select all

public static void main(String args[])
	{
		final byte b=10;
		char ch='c';
		byte cb='b';
		
		switch(ch)
		{
		case'a':System.out.println("a");break;
		case b:System.out.println("b");break;
		}
	}
	
I am not sure why I wrote that it won't compile. May be I didn't see that b is final. ch is a char and b is a final byte, so it should compile.

Code: Select all

public static void main(String args[])
    {
        call(10);
    }
    /*static void call(int i)
    {
        System.out.println("Int primitive");
    }*/
    static void call(short s)
    {
        System.out.println("short primitive");
    }
    static void call(byte b)
    {
        System.out.println("byte primitive");
    }
10 is definitely an int but we don't have call(int i) method, right? (It's commented out). But 10 cannot be narrowed to short or byte during method call. So, neither of the methods call(byte) and call(short) are applicable here. Hence, compilation failure.

Code: Select all

   char ch = 'x';
        switch (ch) {
            case -1: System.out.println(-1); break;
            default: System.out.println("default");
        }
You are right. The explanation can be improved regarding this code.

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

Post Reply

Who is online

Users browsing this forum: No registered users and 58 guests