1.

When double-type is preferred over float-type in Java?

Answer»

The double-type and the FLOAT-type are both used to represent floating point numbers in Java. However, for some situations double-type is better and in some cases float-type is better.

Double-type is preferred over float-type if more precise and accurate result is required. The precision of double-type is up to 15 to 16 decimal points while the precision of float type is only around 6 to 7 decimal digits.

Another reason that double-type may be preferred over float-type is that it has a larger range. It uses 1 bit for sign, 11 bits for exponent and 52 bits for MANTISSA while float-type only uses 1 bit for sign, 8 bits for exponent and 23 bits for mantissa.

A program that demonstrates double-type and float-type in Java is given as FOLLOWS:

public class Demo {    public static void main(String []args)    {        double d = 55.637848675695785;        float f = 25.657933f;        System.out.println("Value of double = " + d);        System.out.println("Value of float = " + f);    } }

The OUTPUT of the above program is as follows:

Value of double = 55.637848675695786 Value of float = 25.657932


Discussion

No Comment Found