Page 1 of 1

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

Posted: Fri Jan 18, 2013 4:30 am
by icepeanuts
I don't understand the answer for the last question (i.e., c *= i; ). My understanding is it means c = c * i;, of which the result may be beyond of the max value of a char.

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

Posted: Fri Jan 18, 2013 12:59 pm
by admin
c *= i; actually translates to c = (char) c*i;
So it is ok because of the explicit cast.

HTH,
Paul.

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

Posted: Sun Jan 20, 2013 1:06 am
by icepeanuts
Yeah, you are right. But one thing I want to point out is it will lose precision if the actual result of c*i is greater than the max value of a char. Am I correct?

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

Posted: Sun Jan 20, 2013 7:44 am
by admin
You are absolutely correct :)

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

Posted: Thu Jan 16, 2014 4:17 am
by kecker
Why is there an explicit cast for the last one?

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

Posted: Thu Jan 16, 2014 5:46 am
by admin
Where do you see an explicit cast in the options?

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

Posted: Thu Jan 16, 2014 11:07 am
by kecker
admin wrote:Where do you see an explicit cast in the options?
I saw it in your explanation above in explaining why the correct options are correct.
c *= i; actually translates to c = (char) c*i;
So it is ok because of the explicit cast.

HTH,
Paul.

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

Posted: Thu Jan 16, 2014 1:02 pm
by admin
That is because JLS defines a compound operator that way. Please see this: http://docs.oracle.com/javase/specs/jls ... ls-15.26.2

HTH,
Paul.

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

Posted: Thu Mar 26, 2015 4:15 am
by alkour
It is not correct the last sentence of Explanations: 4. c *=i; .... if you use += here, you can't complete line2.

I run c +=i; it is OK and give 'j' char.

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

Posted: Thu Mar 26, 2015 6:20 am
by admin
I am not sure what you mean. += is correct but you can't use it for option 4 because then you will not be able to complete option 2.

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

Posted: Thu May 21, 2015 4:42 am
by subhamsdalmia
Type mismatch: cannot convert from int to Integer
Type mismatch: cannot convert from int to Integer
Cannot invoke shortValue() on the primitive type short
The operator *= is undefined for the argument type(s) char, Integer

at Enthuware.P9.main(P9.java:10)

for,
c*=i;
i =(int)k.shortValue();
Integer i=9;

Code: Select all

public class P9 
{

	public static void main(String[] args)
	{
		short k=9;
		Integer i=9;
		boolean b = false;
		char c='a';
		String str="123";
		
i =(int)k.shortValue();  // You can use *= here but then you can't complete the 4th line.
str += b; //  You can't use =, or *= here. Only += is valid.
b = !b; // You can't use anything other than = here.
c*=i; // You can only use *= or +=. = is not valid. Further, if you use += here, you can't complete line 2. 


	}

}
Is it eclipse problem or something else?

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

Posted: Thu May 21, 2015 4:45 am
by admin
Difficult to say. Please use command line.

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

Posted: Sat Nov 26, 2016 12:30 pm
by phusztek
admin wrote:c *= i; actually translates to c = (char) c*i;
So it is ok because of the explicit cast.

HTH,
Paul.

Actually it still doesn't compile because the precedence of the explicit cast is higher than the * operator. So I think the c *= i; transletes to c = (char) (c*i);

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

Posted: Sat Nov 26, 2016 10:43 pm
by admin
Yes, you are right. It translates to c = (char) (c*i);
Paul.

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

Posted: Tue May 01, 2018 3:19 am
by Neib133
Feedback;
I have to admit that I did not understand this question. It took me a couple of minutes of picking my brain until I managed to re-read the text on the top. I somehow missed that I am supposed to put the pieces together that would work! :S