Java Question: possible lossy conversion from double to int

Related searches

Theres probably a lot of things wrong with this code but I can probably handle the rest, I just need to know how to fix the error "incompatible types: possible lossy conversion from double to int"

public class assignment5_3
{
   public static void main(String[] args)
   {    
      //System.out.println("The falling distance is "+d+" when time is "+t);
      for (int t = 1; t < 10; t++)
      {
         double result = fallingDistance();
      }
   }
   public static int fallingDistance()
   {
      double g = 9.8;
      int t = 5; 
      double d = (1/2)*g*t*t;
      return(d);

   }
}

Your return type of function fallingDistance() should be double.

public static double fallingDistance()
   {
      double g = 9.8;
      int t = 5; 
      double d = (1/2)*g*t*t;
      return(d);

   }

Problem regarding possible lossy conversion from double to int , * and open the template in the editor. */ package debugothercode; import java.util .Scanner; /** * * @author Hemant Dhanuka */ class TWONMS {� You need to review your data types:. int and long are for whole numbers only.. float and double are for decimal numbers. So, when you write. int birthrate = 1.0/7.0; Java will complain that it is a lossy conversion because you lose all the decimals and the result will just simply be 0 (because 7 fits in 1 exactly 0 times - integer division - primary school maths).

The problem with your code is that it is expecting an integer value in return statement but you are return a double which is why it is throwing the error. Go through this link to understand this better.

Possible Lossy Conversion from Double to Int : learnjava, Possible Lossy Conversion from Double to Int Is it because javascript is more artsy and java is more mathy? So my question is, is it meant for everyone? Answer to question 1. There is a loss of precision according to the compiler, because the compiler only sees that you have an int variable on the left and a double value on the right.

Any of the following will resolve the issue:

A. Change the return type to double because the variable, d is of type double

public static double fallingDistance() {
    double g = 9.8;
    int t = 5;
    double d = (1 / 2) * g * t * t;
    return d;
}

B. Cast the variable, d into int to match its type with the return type

public static int fallingDistance() {
    double g = 9.8;
    int t = 5;
    double d = (1 / 2) * g * t * t;
    return (int) d;
}

However, note that you will lose the decimal part in the second case i.e. only the integer part of the value of the variable, d will be returned.

By the way, you do not need parenthesis around the variable, d in any case.

You would also like to check Java JDK - possible lossy conversion from double to int to understand the reason for possible lossy conversion from double to int.

Incompatible types: possible lossy conversion from double to int , Incompatible types: possible lossy conversion from double to int Before you laugh at my most likely horrible code, I know I'm terrible at java. Anyway, the error in question appears on lines 21, 23, and 25, which for future reference is� It seems it returns the result as the double. You then try to return the double as the int, and that is a incorrect conversion. So the solutions is as below: return b * b * b; // i.e. please don't use `pow`. or return (int) Math.pow(b, 3); Hope this solution solves your problem.

Lossy Conversion in Java, The double values can be too large or too small for an int and decimal values will get lost in the conversion. Hence, it is a potential lossy� Conversion of int to byte. i = 257 b = 1 Conversion of double to byte. d = 323.142 b = 67 Type promotion in Expressions. While evaluating expressions, the intermediate value may exceed the range of operands and hence the expression value will be promoted. Some conditions for type promotion are:

Solution : The very first problem I can see is the simple typo. Java is case sensitive language, so “cube” and “Cube” mean completely different things. Solution: 1)� The problem's not with the method but the way you're trying to invoke it. If you say 4, Java will think, okay, this is an int. If you say 4L, Java will say, I see you want to have a long, so that's going to be a long. If you say 4.0, Java will think, okay, this is a double.

I am very new to Java and trying to learn arrays in Java. I am trying to enter one int and the other long ex: 1 and 1000000000 and now I am trying to create the array of size 1000000000. And after creating the array at each index of my array willing to store int val, ex: arr[100000000] = 4.

Comments
  • d is a double, but your method returns an int, change the return type of fallingDistance to double
  • Note that (1/2) is integer division, The result of this is 0, not 0.5. You probably meant to do double division, e.g. by doing 1.0 / 2.0. And there isn't really a point in having those variables. Just do return 1.0 / 2.0 * 9.8 * 5 * 5; or better, have it as constant private static final int FALLING_DISTANCE = ...;.
  • You probably intended to have the method accept a parameter int t and then call it like fallingDistance(t);. Otherwise it will just always use 5 as time, since that is what you hardcoded in the method. Also, try to avoid abbreviating variable names. Write them out, makes the code much clearer. gravity and time and distance.