Possible lossy conversion from double to int and cannot find symbol
Solution 1
The first problem is a simple typo. Java is case sensitive, so cube
and Cube
mean different things. Solution: 1) be consistent, and 2) use names starting with lowercase letters for method names ... as per the Java style guides.
The second problem is due to the method signature for Math.pow
; see the javadoc. It returns the result as a double
. You then attempte to return the double
as an int
, and that is a lossy conversion.
The solutions include:
return b * b * b; // i.e. don't use `pow`.
or
return (int) Math.pow(b, 3);
The second one directly addresses your compilation error by casting the double
return to an int
. This is the way you tell the compiler that the lossy conversion is actually OK.
The lossy conversion error message you are seeing refers to that fact for large enough values of b
, the result of Math.pow(b, 3)
will be too large to be represented as an int
1. With the type cast in place, Java will convert a floating point number that is "too large" into Integer.MAX_VALUE
.
The first solution is faster and simpler code, but if b
is too large, the calculations will silently overflow and you will get a nonsense answer.
UPDATE - If you wanted overflow to always be treated as an error then:
return Math.multiplyExact(b, Math.multiplyExact(b, b));
or
return Math.toIntExact((long) Math.pow(b, 3));
or variations on the above.
1 - Actually, the compiler doesn't know about the semantics of pow
, so it doesn't know about "... for large enough values of b
...". But it does know that if the method call did return a large enough value, then the conversion would be lossy. That's why the message says "possible lossy conversion".
Solution 2
Two problems:
- Letter case is important:
Cube
is not the same ascube
. - Your function
cube(int)
returns an integer, but the output ofMath.pow
is adouble
.
You should probably change cube()
to return a double
.
Then you have more code to write in the main
method to use the results of calling cube
.
McDodger
Updated on July 17, 2022Comments
-
McDodger almost 2 years
I'm getting an error in my program saying:
Lyrics.java:11: error: cannot find symbol
Cube(b); ^
symbol: method Cube(int)
location: class Lyrics
Lyrics.java:15: error: incompatible types: possible lossy conversion from double to int
return Math.pow (b, 3); ^
2 errors
I looked around on the website and it turns out other people also had this issue but i looked at the comments and i still dont understand the problem, theres no double in my code why would it say this. And i also dont undestand the error saying cannot find symbol. Please help ive really been stuck on this for a while.
import static java.lang.Math.pow; import java.util.Scanner; public class Lyrics { public static void main(String []args) { int b; Scanner scan = new Scanner(System.in); System.out.println ("Enter a number: "); b = scan.nextInt(); Cube(b); } public static int cube (int b) { return Math.pow (b, 3); } }
-
McDodger over 8 yearsthank you so much, but do you understand the other error?
-