Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java allows implicit conversion of int to float. Why?

Tags:

java

In Java, we can convert an int to float implicitly, which may result in loss of precision as shown in the example code below.

public class Test {
    public  static void main(String [] args) {
        int intVal = 2147483647;
        System.out.println("integer value is " + intVal);
        double doubleVal = intVal;
        System.out.println("double value is " + doubleVal);
        float floatVal = intVal;
        System.out.println("float value is " + floatVal);
        }
}

The output is

integer value is 2147483647
double value is 2.147483647E9
float value is 2.14748365E9

What is the reason behind allowing implicit conversion of int to float, when there is a loss of precision?

like image 249
KRiSHNA Avatar asked Aug 10 '12 19:08

KRiSHNA


People also ask

Can int be converted to float?

To convert an integer data type to float you can wrap the integer with float64() or float32. Explanation: Firstly we declare a variable x of type int64 with a value of 5.

What is implicit type conversion in Java?

The process of converting one type of object and variable into another type is referred to as Typecasting. When the conversion automatically performs by the compiler without the programmer's interference, it is called implicit type casting or widening casting.

Why would you use a float in Java?

The default value of a float in Java is 0.0f. Float data type is used when you want to save memory and when calculations don't require more than 6 or 7 digits of precision.

Which method is used to convert integer data type to float?

Integer floatValue() Method in Java Return Type: A numeric value represented by this object after converting it to type float.


2 Answers

Converting an integer type to a floating point type that uses the same number of bits may result in a loss of precision, but will be done automatically. "Loss of precision" means that some of the less significant digits may become zeros, but the most important digits and the size of the number will remain. Recall that float has only about seven decimal digits of precision. For example, converting the int 123456789 to a float 123456700.0 shows a loss of precision.

like image 33
altini Avatar answered Oct 24 '22 04:10

altini


You are probably wondering:

Why is this an implicit conversion when there is a loss of information? Shouldn't this be an explicit conversion?

And you of course have a good point. But the language designers decided that if the target type has a range large enough then an implicit conversion is allowed, even though there may be a loss of precision. Note that it is the range that is important, not the precision. A float has a greater range than an int, so it is an implicit conversion.

The Java specification says the following:

A widening conversion of an int or a long value to float, or of a long value to double, may result in loss of precision - that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode.

like image 81
Mark Byers Avatar answered Oct 24 '22 05:10

Mark Byers