The interal value of 'b' is 98, which is less than 127 so Line //1 is fine.
About Question enthuware.ocajp.i.v7.2.986 :
Moderator: admin
-
- Posts: 4
- Joined: Fri Oct 18, 2013 2:26 am
- Contact:
About Question enthuware.ocajp.i.v7.2.986 :
Typo in the explanation:
-
- Posts: 3
- Joined: Sat Nov 30, 2013 1:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
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.
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
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?
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?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
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.
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
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
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
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
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.
-
- Posts: 8
- Joined: Thu Dec 07, 2017 4:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
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 ??
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
Is this allowed ?
Code: Select all
case '£': // 1
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
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
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
I wanted to try a charachter that has ASCII value above 127:
The error is the one above.
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
}
}
}
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
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
}
}
}
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
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
Also this one:
Error:
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
}
}
}
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
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
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.
-
- Posts: 23
- Joined: Fri Mar 26, 2021 7:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
the case 'b' is not consistent with byte x .
Why can it compile?
Why can it compile?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
Why do you think it is not?? And what do you mean by "consistent" just to make sure we understand what you are asking?baichen7788 wrote: ↑Sun Jun 13, 2021 3:39 amthe case 'b' is not consistent with byte x .
Why can it compile?
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Mar 26, 2021 7:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
x is byte, while case 'b' is char.
why can it compile?
why can it compile?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.986 :
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
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.
Who is online
Users browsing this forum: No registered users and 30 guests