Page 1 of 1

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

Posted: Fri Oct 18, 2013 1:30 pm
by mbrichmo
Typo in the explanation:
The interal value of 'b' is 98, which is less than 127 so Line //1 is fine.

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

Posted: Sat Nov 30, 2013 1:21 pm
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.

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

Posted: Sun Aug 09, 2015 8:19 am
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?

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

Posted: Sun Aug 09, 2015 8:34 am
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.

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

Posted: Sun Aug 09, 2015 9:58 am
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

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

Posted: Sun Aug 09, 2015 10:10 am
by admin
And also, case values have to be constants. c is a variable.

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

Posted: Thu Dec 07, 2017 9:28 am
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 ??

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

Posted: Thu Dec 07, 2017 9:15 pm
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.

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

Posted: Sun Jul 08, 2018 7:58 am
by flex567
Is this allowed ?

Code: Select all

case '£':   // 1

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

Posted: Sun Jul 08, 2018 10:36 am
by admin
What happened when you tried it out?

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

Posted: Sun Jul 08, 2018 12:45 pm
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 ?

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

Posted: Sun Jul 08, 2018 7:50 pm
by admin
Not sure what you are trying to do. Please post the complete code that you are trying to compile.

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

Posted: Mon Jul 09, 2018 12:12 pm
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.

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

Posted: Mon Jul 09, 2018 12:14 pm
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

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

Posted: Mon Jul 09, 2018 12:15 pm
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

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

Posted: Mon Jul 09, 2018 12:37 pm
by admin
You can't have a case value that doesn't fit into the switch variable.

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

Posted: Sun Jun 13, 2021 3:39 am
by baichen7788
the case 'b' is not consistent with byte x .
Why can it compile?

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

Posted: Sun Jun 13, 2021 7:48 am
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?

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

Posted: Wed Jun 16, 2021 2:31 am
by baichen7788
x is byte, while case 'b' is char.

why can it compile?

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

Posted: Wed Jun 16, 2021 3:40 am
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