About Question enthuware.ocajp.i.v7.2.1045 :
Moderator: admin
- 
				vchhang
 - Posts: 36
 - Joined: Tue May 06, 2014 8:30 am
 - Contact:
 
About Question enthuware.ocajp.i.v7.2.1045 :
I can't seem to find anywhere in the JLS that states the default statement must be the last label.
			
			
									
									
						- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1045 :
Because it can be anywhere. That is why it is not a correct option.vchhang wrote:I can't seem to find anywhere in the JLS that states the default statement must be the last label.
- 
				yegomosc
 - Posts: 1
 - Joined: Thu Feb 11, 2016 2:49 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1045 :
Hi,
the above code piece of code had to contains expliciite casting to byte
but in the Line //1 it was not required (Why??
 )
I couldn't find the explanation, why it is possible that it worked?
			
			
									
									
						Code: Select all
 public static void main(String[] args) {
        char a = 'a';
        byte b = (byte) a;
    }
but in the Line //1 it was not required (Why??
Code: Select all
void test(byte x) {
        switch (x) {
            case 'a':   // 1
            case 256:   // 2
            case 0:     // 3
            default:   // 4
            case 80:    // 5
        }
    }
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1045 :
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).
			
			
									
									
						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).
- 
				NickWoodward
 - Posts: 29
 - Joined: Mon Mar 30, 2015 6:00 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1045 :
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
			
			
									
									
						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: 29
 - Joined: Sun Feb 18, 2024 7:21 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1045 :
When I try to cast it to a byte, I get error
java: duplicate case label
I don't understand why.
			
			
									
									
						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: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1045 :
(byte) 256  is 0.
			
			
									
									
						- 
				likejudo
 - Posts: 29
 - Joined: Sun Feb 18, 2024 7:21 pm
 - Contact:
 
Who is online
Users browsing this forum: No registered users and 57 guests