• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

no compile due to loss of precision

 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have these lines in my code.

public class MyClass{
byte completion;

public MyClass(){
completion = 0;
}

public void write(int nextBits, int bitNum){
byte btw = 8 - completion;
}
}

Of course there is much more, but this is the important part.
I keep getting the error message seido ga ochiteiru kanosei, which in English means something like "There is a possibility of loss of precision" and another message which tells me that the program is expecting a byte but getting an int. So it won't compile.
I get this message for several lines, but this is the first line.
I don't see where the int is. btw and completion are both bytes. I thought that the program is treating 8 as an int, so I tried this line

byte eight = (byte) 8;

and then changed the other line to

byte btw = eight - completion;

but I got the same error message.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to cast the result of the subtraction:
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. But I wonder why you have to cast the result to a byte. Does the subtraction operation automatically turn every byte into an int?
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But I wonder why you have to cast the result to a byte. Does the subtraction operation automatically turn every byte into an int?



No the subtraction operator won't do that.
This is because when you cast from a higher type (in this case an int) to lower(byte) the compiler expects that you know that you know what you are doing and hence it requires an explicit cast.


Hope this helps
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The result of every arithmetic operation in Java will be at least int, never short, char or byte.
 
Attractive, successful people love this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic