Why float to int is not implicitly converted in Java when both uses 4 bytes in memory?

122 Views Asked by At

Why is it so that both float and int requires 4 bytes of memory storage, but int can be converted to float and float cannot to int? Please explain in terms of memory storage, like what happens in the memory storage?

class A5 {

    public static void main(String[] args) {
     
        float f = 10.5f;
        int a = f;
        System.out.println(a);
}

it gave error like this->

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
        Type mismatch: cannot convert from float to int

        Syntax error, insert "}" to complete ClassBody
            at A5.main(A5.java:6)
2

There are 2 best solutions below

0
Mark Rotteveel On

A conversion from float to int is lossy both in precision, but also in range (magnitude).

On the precision side, storing 10.5f in an int would make you lose the fractional value, but the range is probably even more relevant. An int can store [-2147483648, 2147483647], while a float can store numbers roughly in the range [-3.4e38, 3.4e38]. In other words, float has a lot of numbers you cannot just store in an int without some form of overflow.

So, because you could lose precision and/or have overflow, the Java compiler doesn't allow you to assign a float to an int without explicitly signalling that you accept the consequences. And you signal that by using an explicit cast (in your example, you would need to use int a = (int) f;).

Although float and int are both 4 bytes, their representation is entirely different. If you would want to know "what is the equivalent int value for the 4 bytes of a float", you will need to use Float.floatToIntBits(float), but the integer value you'll receive will have no (seeming) correlation to the original float value (e.g. Float.floatToIntBits(10.5f) produces the integer 1093140480).

0
Manuna Mady On

The reason why both float and int require 4 bytes of memory storage but int can be converted to float and float cannot to int is because of the way that float and int numbers are represented in memory.

Integers are represented in memory as a sequence of bits, where each bit represents a single digit in the number. For example, the integer 10 would be represented in memory as the following sequence of bits:

00000000 00000000 00000000 00001010

Floats are represented in memory using a different format. Floats use a special format called IEEE 754. IEEE 754 allows floats to represent a wider range of numbers than integers, but it also requires more memory.

The IEEE 754 format for floats is as follows:

Sign bit: 1 bit

Exponent: 8 bits Mantissa: 23 bits

The sign bit indicates whether the number is positive or negative. The exponent controls the magnitude of the number. The mantissa represents the fractional part of the number.

When an integer is converted to a float, the integer is simply placed in the mantissa of the float. This means that the float will lose any precision that is beyond the 23 bits of the mantissa.

However, when a float is converted to an integer, it is not possible to recover the lost precision. This is why the compiler will not allow you to convert a float to an integer without explicitly casting it.

In the code snippet that you provided, the compiler is preventing you from converting the float f to the integer a because it would result in data loss.

If you need to convert a float to an integer, you can use the following code:

int a = (int) f;

This will cast the float f to an integer, and the compiler will round the number to the nearest integer.

However, it is important to note that this can result in inaccurate results, especially if the float number is close to a boundary between two integers.