Solved Java error: possible loss of precision

March 23, 2013 at 13:39:00
Specs: Windows 7, Intel(R) Core(TM) i3 / 3.20 GHz / 8 GB Ram
Hi, I am VERY new to Java so this will probably be easy to answer.

I am learning Java and I am during some "exercises" and I am making a test tv and I am stuck.

I get these wierd errors when I try to compile:

C:\Users\****\Desktop\Java\TV.java:10: error: possible loss of precision
cValue = cValue - 1;
^
required: byte
found: int
C:\Users\****\Desktop\Java\TV.java:28: error: possible loss of precision
sound = sound + sValue;
^
required: byte
found: int
C:\Users\****\Desktop\Java\TV.java:40: error: possible loss of precision
sound = sound - sValue;
^
required: byte
found: int
C:\Users\****\Desktop\Java\TV.java:51: error: possible loss of precision
fChannel = channel + 1;
^
required: byte
found: int
4 errors
Any help would be appreciated.


See More: Java error: possible loss of precision

Report •


#1
March 23, 2013 at 15:28:55
Is there any reason you're using byte and not int?
As is, you'll need to cast your values to byte.
cValue = cValue - (byte)1;

How To Ask Questions The Smart Way


Report •

#2
March 24, 2013 at 00:48:49
Eh, I thought that it would be more easy to load since that I dont need higher numbers.

But if thats the case then can you explain whats going on and why I need int.

Becouse that I am new to Java it would help if you explained it :)


Report •

#3
March 24, 2013 at 08:18:51
✔ Best Answer
Eh, I thought that it would be more easy to load since that I dont need higher numbers.
1) CPUs work best in their default bit width, typically an integer. Anything else is going to require more work at some point.
2) You're writing in Java, the language whose main selling point was that it wasn't tied to any hardware architecture. Java tries to abstract away the hardware, so do it a favor and stop thinking about the hardware.
3) You're showing signs of the premature optimization anti-pattern. Remember the 40-year-old mantra Premature optimization is the root of all evil.

can you explain whats going on and why I need int.
Not without breaking into binary math, but I'll try.

So you try to add a byte and an int.
Well, they need to be the same type before you can perform arithmetic, so Java converts the byte to an int. This is guaranteed to be safe, because ints are larger than bytes.
Good, now that we have two numbers of the same type (int), we can add them. An int plus an int results in an int.
what do we do with this int? Why, stuff it in a byte, because that's the type left of the equals sign.
Java's byte's range is -128 to 127, but Java's int's range is -2,147,483,648 to 2,147,483,647. That means our int can contain 4,294,967,040 values our byte cannot hold.
The solution? Complain during the compile.

The workaround is to explicitly convert the int to a byte, because adding a byte to a byte is legal without further conversion, and explicitly converting an int to a byte is legal. The smarter workaround is to just use ints, because they're going to spend some portion of their lives as ints anyways.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
March 24, 2013 at 12:05:45
I do know binary so I got the point and I know that stuff. Just forgot, dang it.

But why would you even use byte in the first place then?


Report •

#5
March 24, 2013 at 12:22:20
'Cause the String class has to use something. I'm only half joking.

How To Ask Questions The Smart Way


Report •

Ask Question