The Artima Developer Community
Sponsored Link

Java Answers Forum
lang design question

1 reply on 1 page. Most recent reply: Apr 24, 2003 11:29 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
jay raymond

Posts: 1
Nickname: vjraman
Registered: Apr, 2003

lang design question Posted: Apr 24, 2003 10:51 AM
Reply to this message Reply
Advertisement
hi java newbie curious why

java (compiler) complains about loss of precision with

byte = byte * byte, but not with int = int * int.

i.e. byte b1 = 40; byte b2=8; b1=b1*b2;

vs.

int i = Integer.MAX_VALUE; int j=4; i=i*j;

i'm probably missing something.
vj


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: lang design question Posted: Apr 24, 2003 11:29 AM
Reply to this message Reply
This is another good reason to use the Jikes compiler instead of Javac: besides being drastically faster, it gives better and more detailed error messages. (You can easily find the Jikes compiler by searching on Google).

When you multiply the two bytes, the temporary result is an int. That is, the bytes are promoted to ints in order to perform the multiplication. You can find plenty of details about this on chapters of Inside the Java Virtual Machine book on this web site.

The real problem is that you are assigning a int value to a byte variable. Notice that you get the same error message from this code:
int i = 1;
byte b = i;

So really all you need to do is cast the int back to a byte, by doing something like this:
byte a = 1, b = 2;
byte c = (byte)(a*b);

Flat View: This topic has 1 reply on 1 page
Topic: B_TREES Previous Topic   Next Topic Topic: EJb

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use