Page 1 of 2

About Question enthuware.ocajp.i.v7.-2-.1357 :

Posted: Tue Jul 15, 2014 3:37 am
by kashyapa
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.

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

Posted: Tue Jul 15, 2014 11:16 am
by admin
What happened when you tried it out?

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

Posted: Wed Jul 16, 2014 7:56 am
by kashyapa
try it you self ;)

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

Posted: Wed Jul 16, 2014 8:42 am
by admin
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.

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

Posted: Wed Jul 16, 2014 8:44 am
by admin
I see that you've updated your post and removed the question.

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

Posted: Wed Jul 16, 2014 9:34 am
by kashyapa
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.

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

Posted: Sat Oct 25, 2014 2:22 pm
by Tony.Singarayar
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

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

Posted: Sat Oct 25, 2014 7:00 pm
by admin
Yes, you can pass any value. If it doesn't match any label, the control will go to default.

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

Posted: Sun Oct 26, 2014 3:13 pm
by Tony.Singarayar
Hi,
How can the char value be -655454 when char is unsigned?

Can you please explain.

THanks,
Tony Singarayar

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

Posted: Sun Oct 26, 2014 7:17 pm
by admin
You asked what if you pass an int, not char. Int can be compared to char labels.

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

Posted: Sat Feb 07, 2015 9:54 am
by gparLondon
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,

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;
		}
	}
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,

Code: Select all

switch(ch)
{
case 10:break;//fine
case -20://compilation  fails
}
Regards,
GPAR

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

Posted: Sat Feb 07, 2015 12:32 pm
by admin
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.

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

Posted: Sat Feb 07, 2015 2:01 pm
by gparLondon
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?
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.
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}).

Thanks for your kind help,
GPAR

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

Posted: Sat Feb 07, 2015 8:52 pm
by admin
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.

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

Posted: Sun Feb 08, 2015 6:29 am
by gparLondon
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

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

Posted: Tue Feb 10, 2015 2:32 pm
by gparLondon
Hi Paul,

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");
	}
	
}
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

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

Posted: Tue Feb 10, 2015 10:02 pm
by admin
Because as per Section 5.3 of JLS 7, narrowing conversions are not allowed in method calls.

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

Posted: Tue Apr 19, 2016 8:55 am
by levijatanus
Can you explain how come this is ok and prints

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");
		}

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

Posted: Tue Apr 19, 2016 9:10 am
by admin
Why do you think it should not be ok?
Paul.

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

Posted: Wed Apr 20, 2016 1:51 am
by levijatanus
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:

Code: Select all

This will not work in all cases because a byte may have negative values which cannot be assigned to a char.
So if assigning is happening how come cast is not needed?

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

Posted: Wed Apr 20, 2016 4:05 am
by admin
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.

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

Posted: Sat Sep 30, 2017 3:56 am
by zoharch
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;
}

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

Posted: Tue Feb 27, 2018 9:06 am
by Meghana
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.

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

Posted: Tue Feb 27, 2018 9:19 am
by admin
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).

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

Posted: Fri Mar 02, 2018 12:58 pm
by Meghana
I got it. Thank you. :) Will run and check.
It was basic! just got carried away by the "negative sign"