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

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

Moderator: admin

Post Reply
mbrichmo
Posts: 4
Joined: Fri Oct 18, 2013 2:26 am
Contact:

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

Post by mbrichmo »

Typo in the explanation:
The interal value of 'b' is 98, which is less than 127 so Line //1 is fine.

andgvoz
Posts: 3
Joined: Sat Nov 30, 2013 1:14 pm
Contact:

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

Post by andgvoz »

The char 'b' saved in memory with the same combination of zeros and ones as 98. The biggest value byte type can store is 127.

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

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

Post by sir_Anduin@yahoo.de »

Hi,
I am having a hard time understanding why:

public static void main(String[] args)
{
byte x = 1;
char c = 'a';
switch (x)
{
case c: // 1
case 'b': // 2
}
}

//1 is a compile time error
// 2 is okey

both are chars am I right?

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

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

Post by admin »

c is a char . char is a larger data type than byte, so it is not possible for all the values of c to match the values of x. Java designers decided to disallow this to avoid such confusing construct. There is no special technical reason but a judgement call by the designers.
If you like our products and services, please help us by posting your review here.

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

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

Post by sir_Anduin@yahoo.de »

thanks.

just as an addition:
if using a char >8 bit, it will not compile:
case '§':

also it will compile if char c with the value of 'a' if final

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

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

Post by admin »

And also, case values have to be constants. c is a variable.
If you like our products and services, please help us by posting your review here.

yassine
Posts: 8
Joined: Thu Dec 07, 2017 4:43 am
Contact:

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

Post by yassine »

in this example to decide which case to choose, does java "auto-promote" the byte and char values to int and then decide, or does it 'auto-box" byte to char ??

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

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

Post by admin »

Since case values are always constants and are always integral, numeric promotion doesn't apply here. If you have two case values that are same e.g. one is 97 and one is 'a', it will not compile because their value is same. So there is no issue in deciding whether to use char or byte. They are both integral values.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

Is this allowed ?

Code: Select all

case '£':   // 1

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

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

Post by admin »

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

Code: Select all

Temp.java:9: error: unclosed character literal
			case 'Ôé¼':   // 1
			     ^
Temp.java:9: error: illegal character: '\u201a'
			case 'Ôé¼':   // 1
			       ^
Temp.java:9: error: illegal character: '\u00ac'
			case 'Ôé¼':   // 1
			        ^
Temp.java:9: error: unclosed character literal
			case 'Ôé¼':   // 1
			         ^
Temp.java:9: error: ';' expected
			case 'Ôé¼':   // 1
			           ^
Temp.java:10: error: illegal start of expression
			default :   // 2
			        ^
Temp.java:10: error: ';' expected
			default :   // 2
			         ^
Temp.java:11: error: not a statement
			case -2:    // 3
			     ^
Temp.java:11: error: ';' expected
			case -2:    // 3
			       ^
9 errors
Because it is byte in the switch it only accepts values from - 128 to 127 ?

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

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

Post by admin »

Not sure what you are trying to do. Please post the complete code that you are trying to compile.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

I wanted to try a charachter that has ASCII value above 127:

Code: Select all

public class Temp{
   
   public static void main (String args[]){
   }
   public void switchTest(byte x){
		switch(x){
			case '€':   // 1 
			default :   // 2
			case -2:    // 3
			case 80:    // 4
		}
	}
} 
The error is the one above.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

Code: Select all

public class Temp{
   
   public static void main (String args[]){

   }
   public void switchTest(byte x){
		switch(x){
			case '£':   // 1 (extended ASCII 156)
			default :   // 2
			case -2:    // 3
			case 80:    // 4
		}
	}
} 
Error:

Code: Select all

Temp.java:9: error: unclosed character literal
			case '£':   // 1
			     ^
Temp.java:9: error: unclosed character literal
			case '£':   // 1
			        ^
Temp.java:9: error: not a statement
			case '£':   // 1
			       ^
3 errors

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

Also this one:

Code: Select all

public class Temp{
   
   public static void main (String args[]){
   
   }
   public void switchTest(byte x){
		switch(x){
			case 'Đ':   // 1 (extended ASCII 209)
			default :   // 2
			case -2:    // 3
			case 80:    // 4
		}
	}
} 
	
Error:

Code: Select all

Temp.java:9: error: unmappable character for encoding Cp1252
			case '─?':   // 1 (extended ASCII 209)
			       ^
Temp.java:9: error: unclosed character literal
			case '─?':   // 1 (extended ASCII 209)
			     ^
Temp.java:9: error: unclosed character literal
			case '─?':   // 1 (extended ASCII 209)
			        ^
3 errors

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

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

Post by admin »

You can't have a case value that doesn't fit into the switch variable.
If you like our products and services, please help us by posting your review here.

baichen7788
Posts: 23
Joined: Fri Mar 26, 2021 7:25 am
Contact:

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

Post by baichen7788 »

the case 'b' is not consistent with byte x .
Why can it compile?

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

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

Post by admin »

baichen7788 wrote:
Sun Jun 13, 2021 3:39 am
the case 'b' is not consistent with byte x .
Why can it compile?
Why do you think it is not?? And what do you mean by "consistent" just to make sure we understand what you are asking?
If you like our products and services, please help us by posting your review here.

baichen7788
Posts: 23
Joined: Fri Mar 26, 2021 7:25 am
Contact:

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

Post by baichen7788 »

x is byte, while case 'b' is char.

why can it compile?

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

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

Post by admin »

It is allowed to assign a char value to a byte if the numeric value of the char fits into a byte (i.e. it is within -128 to 127, 'c' is 99) as both byte and char are integral types.

Please go through a good Java book to know all the rules regarding primitive data types, their assignments, numeric promotion, and casting numeric values. This is important for the exam.
This is also covered in chapter 3 and 6 of https://amzn.to/3sJkGyO
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 39 guests