[Bluej-discuss] Compile Error for correct code.
Louise Laforest
laforest.louise at uqam.ca
Tue Mar 7 13:02:36 GMT 2006
Just a detail, parenthesis are needed here because type cast has
higher precedence than multiplication :
b = (byte) ( b * 2 );
Louise
Le 06-03-07 à 05:29, Todd O'Bryan a écrit :
> The short answer is, don't use bytes unless you have a very good
> reason to. Stick to ints.
>
> The long answer is that b is a byte, while 2 (and every other non-
> decimal number you just enter into Java code) is an int. A byte
> times an int gives an int, but you try to stick that new int you
> just created back into a byte-sized variable. This could cause
> problems, and the compiler is warning you about that. If you write,
>
> b = (byte) b * 2;
>
> instead, the compiler will stop complaining because the cast
> convinces it that you know there could be a problem and you're okay
> with that.
>
> Todd
>
> On Mar 7, 2006, at 2:34 AM, Namit TClaw wrote:
>
>> Hello, this is Namit here.
>>
>> I am getting an error in the code here:
>>
>>
>>
>> public class Test
>>
>> {
>>
>> public static void display()
>>
>> {
>>
>> byte b=50;
>>
>> b=b*2;
>>
>> System.out.println(b);
>>
>> }
>> }
>>
>>
>>
>> The Error shown is:possible loss of precision in the line:b=b*2;
>>
>> A peculiar feature is that if I write : b*=2 , it works perfectly
>> fine.
>>
>>
>>
>> I don’t know what is the reason for this. Can anyone
>> explain why it is happening???
>>
>>
>>
>>
>>
>> Regards,
>>
>> Namit.
>>
>> _______________________________________________
>> mailing list bluej-discuss at bluej.org
>> To unsubscribe or change your preferences, go to
>> http://lists.bluej.org/mailman/listinfo/bluej-discuss
>
> _______________________________________________
> mailing list bluej-discuss at bluej.org
> To unsubscribe or change your preferences, go to
> http://lists.bluej.org/mailman/listinfo/bluej-discuss
-----------------------------------------
Louise Laforest, Ph.D.
Professeure
Université du Québec à Montréal
Département d'informatique
téléphone (514) 987-3000 poste 7790
fax (514) 987-8477
laforest.louise at uqam.ca
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.bluej.org/pipermail/bluej-discuss/attachments/20060307/782d4da7/attachment-0001.html
More information about the bluej-discuss
mailing list