About Question enthuware.ocajp.i.v7.2.1089 :
Moderator: admin
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1089 :
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
c *= i; actually translates to c = (char) c*i;
So it is ok because of the explicit cast.
HTH,
Paul.
So it is ok because of the explicit cast.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
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?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
You are absolutely correct
If you like our products and services, please help us by posting your review here.
-
- Posts: 18
- Joined: Mon Jan 13, 2014 5:39 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
Why is there an explicit cast for the last one?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
Where do you see an explicit cast in the options?
If you like our products and services, please help us by posting your review here.
-
- Posts: 18
- Joined: Mon Jan 13, 2014 5:39 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
I saw it in your explanation above in explaining why the correct options are correct.admin wrote:Where do you see an explicit cast in the options?
c *= i; actually translates to c = (char) c*i;
So it is ok because of the explicit cast.
HTH,
Paul.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 30
- Joined: Tue Mar 24, 2015 2:59 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
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.
I run c +=i; it is OK and give 'j' char.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Sat May 02, 2015 11:57 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
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;
Is it eclipse problem or something else?
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.
}
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
Difficult to say. Please use command line.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Tue Nov 22, 2016 11:04 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
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);
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
Yes, you are right. It translates to c = (char) (c*i);
Paul.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Tue May 01, 2018 3:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1089 :
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
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
Who is online
Users browsing this forum: No registered users and 77 guests