About Question enthuware.ocajp.i.v7.-2-.1357 :
Moderator: admin
- 
				kashyapa
 - Posts: 23
 - Joined: Thu May 08, 2014 5:27 am
 - Contact:
 
About Question enthuware.ocajp.i.v7.-2-.1357 :
If we change the switch expression to char and the case labels in to int, it will not compile because of every int values cannot be assignable to char.
			
			
													
					Last edited by kashyapa on Wed Jul 16, 2014 7:55 am, edited 1 time in total.
									
			
									
						- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
What happened when you tried it out?
			
			
									
									
						- 
				kashyapa
 - Posts: 23
 - Joined: Thu May 08, 2014 5:27 am
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
try it you self    
			
			
									
									
						- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
We try to help as much as we can but if we start spoon feeding, we won't be able to help anyone. So please help us help you and post the details of your efforts.
			
			
									
									
						- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
I see that you've updated your post and removed the question.
			
			
									
									
						- 
				kashyapa
 - Posts: 23
 - Joined: Thu May 08, 2014 5:27 am
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
Yape… actually what you do in here is precious, we should appreciate that. I got it and apologize.  
  I have already done the OCPJP and I think that’s why I got lazy like this.
			
			
									
									
						- 
				Tony.Singarayar
 - Posts: 3
 - Joined: Sat Oct 25, 2014 2:17 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
Hi,
I still dont understand how "switch expression of type int and case label value of type char" is correct.
What if we pass the vale of int to be -655454. Will there be any char value that can take it...
Please help.
Thanks,
Tony Singarayar
			
			
									
									
						I still dont understand how "switch expression of type int and case label value of type char" is correct.
What if we pass the vale of int to be -655454. Will there be any char value that can take it...
Please help.
Thanks,
Tony Singarayar
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
Yes, you can pass any value. If it doesn't match any label, the control will go to default.
			
			
									
									
						- 
				Tony.Singarayar
 - Posts: 3
 - Joined: Sat Oct 25, 2014 2:17 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
Hi,
How can the char value be -655454 when char is unsigned?
Can you please explain.
THanks,
Tony Singarayar
			
			
									
									
						How can the char value be -655454 when char is unsigned?
Can you please explain.
THanks,
Tony Singarayar
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
You asked what if you pass an int, not char. Int can be compared to char labels.
			
			
									
									
						- 
				gparLondon
 - Posts: 63
 - Joined: Fri Oct 31, 2014 6:31 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
Hi,  
I got this question right, still have couple of doubt regarding this question, Please correct me if I am wrong, I just little help that, how to face such questions in the real exam, I know that case value must be "Compile time constant", hence following program compiles fine,
Then,  how  can  I  prove  option  4  is  wrong?
Secondly, in your explanation for option 4 you have given case value as -1, which is integer compile time constant isn't it? If yes, then it is nothing to do with byte. I do know that in the above program cb=ch or ch=cb is not possible, as per your explanation. Just wanted to know how should we answer such questions in the exam. One last thing, if the option include
"switch expression of type char and case label value of type int".
will be true or false? as,
Regards,
GPAR
			
			
									
									
						I got this question right, still have couple of doubt regarding this question, Please correct me if I am wrong, I just little help that, how to face such questions in the real exam, I know that case value must be "Compile time constant", hence following program compiles fine,
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;
		}
	}Secondly, in your explanation for option 4 you have given case value as -1, which is integer compile time constant isn't it? If yes, then it is nothing to do with byte. I do know that in the above program cb=ch or ch=cb is not possible, as per your explanation. Just wanted to know how should we answer such questions in the exam. One last thing, if the option include
"switch expression of type char and case label value of type int".
will be true or false? as,
Code: Select all
switch(ch)
{
case 10:break;//fine
case -20://compilation  fails
}GPAR
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
In your first code, case b:System.out.println("b");break; should not compile.
Any negative number less than 127 and greater than -128 can be considered a byte if it is a compile time constant. It can also be an int. But that doesn't make the example for option 4 invalid. The fact is byte can take negative values but a char cannot. So, if your switch variable is of type char, your case labels cannot be those bytes/ints that fall outside the range of a char.
The basic idea is that case labels should be assignable to the switch variable. That's all. So if an option makes a general statement that a switch variable of type char and case labels of type ints is good, then it is not valid because as you can see, it will not work for all ints.
Having said that, I would say you need not worry too much about this. If you understand the concept, you will be fine.
HTH,
Paul.
			
			
									
									
						Any negative number less than 127 and greater than -128 can be considered a byte if it is a compile time constant. It can also be an int. But that doesn't make the example for option 4 invalid. The fact is byte can take negative values but a char cannot. So, if your switch variable is of type char, your case labels cannot be those bytes/ints that fall outside the range of a char.
The basic idea is that case labels should be assignable to the switch variable. That's all. So if an option makes a general statement that a switch variable of type char and case labels of type ints is good, then it is not valid because as you can see, it will not work for all ints.
Having said that, I would say you need not worry too much about this. If you understand the concept, you will be fine.
HTH,
Paul.
- 
				gparLondon
 - Posts: 63
 - Joined: Fri Oct 31, 2014 6:31 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
Sorry,  for  raising  the  doubts  again,  I  respect  your  software,  also  have  great  respect  towards  you  as  well,  now  this  code  of  mine,  does  compile.  I  have  no  idea  how  to  prove  it.  Secondly  there  are  compile  time  constant  as  bytes?  I  dint  know  this.  can  you  please  provide  me  the  link  where,  this  is  mentioned?
Thanks for your kind help,
GPAR
			
			
									
									
						As these are the questions which I often get wrong, a easy ones for some one, is tough ones for me. BTW thanks, I got the answer for my last query(i.e switch(char){case:int}).Any negative number less than 127 and greater than -128 can be considered a byte if it is a compile time constant. It can also be an int.
Thanks for your kind help,
GPAR
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
A raw number doesn't have a type. A variable has a type. So if you just write some number, it could very well be a byte, integer, char, or short, etc. assuming that it is within the range of that type. 
For example, byte b = 120; will compile and so will int b = 120; but byte b = 129; will not. So what is 120? is it a byte or is it an int?
case labels are like that. They are just values. Their validity depends on what you are assigning them to i.e. the switch variable.
I am sorry, I don't have any link for this. You may need to google.
			
			
									
									
						For example, byte b = 120; will compile and so will int b = 120; but byte b = 129; will not. So what is 120? is it a byte or is it an int?
case labels are like that. They are just values. Their validity depends on what you are assigning them to i.e. the switch variable.
I am sorry, I don't have any link for this. You may need to google.
- 
				gparLondon
 - Posts: 63
 - Joined: Fri Oct 31, 2014 6:31 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
Hi,  
Thanks for your detailed explanation, after your last post I did google, but could not find anything, but this reply of yours is sufficient to understand the concept.
With respect,
GPAR
			
			
									
									
						Thanks for your detailed explanation, after your last post I did google, but could not find anything, but this reply of yours is sufficient to understand the concept.
With respect,
GPAR
- 
				gparLondon
 - Posts: 63
 - Joined: Fri Oct 31, 2014 6:31 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
Hi  Paul,
According to your explanation on compile time constants in your last post, I wrote this program,
If  the  method  call(int)  is  uncommented  then,  that  method  is  called  else  compile  time  error,  if  10  which  can  fit  into  short/byte,  and  can  be  a  compile  time  constant  of  type  short/byte  as  well  as  int,  then  why  is  this  program  behaves  like  this?  why  cant  it  call  method  call(byte),  which  is  more  specific!  
With respect,
GPAR
			
			
									
									
						According to your explanation on compile time constants in your last post, I wrote this program,
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");
	}
	
}With respect,
GPAR
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
Because as per Section 5.3 of JLS 7, narrowing conversions are not allowed in method calls.
			
			
									
									
						- 
				levijatanus
 - Posts: 3
 - Joined: Sat Apr 16, 2016 7:53 am
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
Can you explain how come this is ok and prints 
One
One
when run
			
			
													One
One
when run
Code: Select all
		final int b1 = 1; 
		final byte i1 = 1;
		switch(i1){
		case 0: System.out.println("Zero");
		case b1: System.out.println("One");
		}
		switch(b1){
		case 0: System.out.println("Zero");
		case i1: System.out.println("One");
		}
					Last edited by levijatanus on Wed Apr 20, 2016 1:52 am, edited 1 time in total.
									
			
									
						- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
Why do you think it should not be ok?
Paul.
			
			
									
									
						Paul.
- 
				levijatanus
 - Posts: 3
 - Joined: Sat Apr 16, 2016 7:53 am
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
I have thought that before case and switch are evaluated some kind of conversion is applied – that variable in case would be casted to type of variable in switch ?
Correlation would be that you can assign byte to int but not vice versa.
In explanation stands:
So if assigning is happening how come cast is not needed?
			
			
									
									
						Correlation would be that you can assign byte to int but not vice versa.
In explanation stands:
Code: Select all
This will not work in all cases because a byte may have negative values which cannot be assigned to a char.- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
Cast is not needed here because the variables are being declared as final. When a variable is declared final the compiler knows that it is never going to change (it is a constant, actually). It looks at the actual value being assigned and determines whether the value is within the limit of the variable or not. If it is, then it accepts the assignment.
Try changing the value being assigned to b1 to (for example) 255 and see what happens.
-Paul.
			
			
									
									
						Try changing the value being assigned to b1 to (for example) 255 and see what happens.
-Paul.
- 
				zoharch
 - Posts: 4
 - Joined: Fri Sep 29, 2017 3:34 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
As I understand, switch expression and case labels can be compiled if:
1.Expression is byte, short,char,int or String.
2. You can convert the syntax to assignment to see if it compiled.
For example:
Switch(ing variable), case with char variable or value like 'c' compiles if also
Int variable = 'c' compiles. And it compiles.
But char k = 100000 will not compile so the following switch will not compile too :
char k;
switch(k) {
case 100000: DOSOMETHING;
}
			
			
									
									
						1.Expression is byte, short,char,int or String.
2. You can convert the syntax to assignment to see if it compiled.
For example:
Switch(ing variable), case with char variable or value like 'c' compiles if also
Int variable = 'c' compiles. And it compiles.
But char k = 100000 will not compile so the following switch will not compile too :
char k;
switch(k) {
case 100000: DOSOMETHING;
}
- 
				Meghana
 - Posts: 29
 - Joined: Sun Feb 11, 2018 3:13 am
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
With respect to option 4: "switch expression of type char and case label value of type byte", is this only in a switch case or can we not even cast a char to int? I tried to google about it. But I couldn't really understand the concept of "special conversion".
Thank you.
			
			
									
									
						Thank you.
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
What happened when you tried it out...assigning a char value to an int? This is actually a very basic thing and you should've been able to answer it right away without writing code to test it. If you are not, you should go through a good book before attempting mock exams.
Hint - char data type (16 bits) is smaller than int (32 bits).
			
			
									
									
						Hint - char data type (16 bits) is smaller than int (32 bits).
- 
				Meghana
 - Posts: 29
 - Joined: Sun Feb 11, 2018 3:13 am
 - Contact:
 
Re: About Question enthuware.ocajp.i.v7.2.1357 :
I got it. Thank you. 
 Will run and check. 
It was basic! just got carried away by the "negative sign"
			
			
									
									
						It was basic! just got carried away by the "negative sign"
Who is online
Users browsing this forum: Google [Bot] and 48 guests