Help for this exam paper Q

Java programming topics
Post Reply
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Help for this exam paper Q

Post by Face » Thu Jul 28, 2011 11:16 am

1.JPG
1.JPG (39.78 KiB) Viewed 4644 times
I cant get understand what is the error of this.I found the answer, :P But can't understand the error.
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: Help for this exam paper Q

Post by Herath » Thu Jul 28, 2011 11:52 am

value is defined a short type variable. But 2 is "int" by default. The error (warning) is loss of precision. Because "int" is 32bit of size. The result of (value*2) is going to be int as I think. Then has to be converted to short in order to be assigned to the "value". So the value has to be cast manually.
possible fixes for this are

Code: Select all

value=(short) ((short) value * (short) 2);

Code: Select all

value*=2;
But you can always cast a 16bit data type in to a 32bit or higher size type without any problem.
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: Help for this exam paper Q

Post by Face » Thu Jul 28, 2011 1:06 pm

thanks brother.!
Post Reply

Return to “Java Programming”