Decimal literals float, double – Computer Sir Ki Class

Code Learning #JAVA#3804

## Decimal literals float, double

Use of decimal/fractional/real literals in its different size as float and double.

## Learning Objectives

• Decimal literal in float and double with its variation of hexadecimal and exponent notation.

## Source Code

 class DecimalLiterals { public static void main(String[] args) { float a=22.345f; double b=22.345; double c=0x22.345P0; double d=22.345E2; double e=22.345E-2; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); System.out.println(e); } } .linedwrap { border: 1px solid #c0c0c0; padding-top: 5px; padding-bottom: 5px; border-radius: 4px; background-color:#F8F8F8; } .linedtextarea { padding: 0px; margin: 0px; background-color:#F8F8F8; } .linedtextarea textarea, .linedwrap .codelines .lineno { font-size: 12pt; font-family: monospace; line-height: normal !important; } .linedtextarea textarea { padding-right:0.3em; padding-top:0.3em; border: 0; } .linedwrap .lines { margin-top: 0px; width: 20px; float: left; overflow: hidden; border-right: 1px solid #c0c0c0; margin-right: 10px; } .linedwrap .codelines { padding-top: 5px; } .linedwrap .codelines .lineno { color:#AAAAAA; padding-right: 2px; padding-top: 0.0em; text-align: right; white-space: nowrap; } .linedwrap .codelines .lineselect { color: red; }

## Run Output

``````22.345
22.345
34.204345703125
2234.5
0.22345
``````

## Code Understanding

float a=22.345f;
The value 22.345f here is a float type literal constant. f is suffixed to differentiate it from double. If you do not suffix it is treated as double value and you can not store it in a float type. Float needs 32 bits of storage

double b=22.345;
Here the value 22.345 is double type literal constant .  Double needs 64 bits of storage.

double c=0x22.345P0;
This is double value shown in hexadecimal notation. This is equivalent to 22 in hex form and then .345 in hex form with Power of 2 as 0 as its multiple.

double d=22.345E2;
This is exponent form which is equivalent to 22.345 x 10^2 which means 2234.5

double e=22.345E-2;
This is negative exponent form which is equilent to 22.345×10^-2 which means 0.22345

System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); System.out.println(e);
Here we print all the decimal equivalents of above.

## Notes

• Exponent notation can allow a value of storage in double data type which is bigger than its no. of bits equivalent.

Suggested Filename(s): DecimalLiterals.java

sunmitra| Created: 11-Mar-2018 | Updated: 11-Mar-2018|

×