Shredator
Shredator

Reputation: 940

Java double initilization with 0

I recently came across some code where double are initialized the following way:

double nb = 0.;

A very similar question has already been asked here but doesn't cover this specific case. Are there any benefits or special behavior of such initialization?

Upvotes: 2

Views: 182

Answers (1)

Federico klez Culloca
Federico klez Culloca

Reputation: 27119

0. is a perfectly legal double, and has the same value as 0.0

You can test it with jshell

jshell> 0. == 0.0
$1 ==> true

Here is the formal specification for how to represent a floating point (i.e. float or double) literal

The gist of it is that these are all valid ways to represent a double

1e1
2.
.3
0.0
3.14
1e-9d
1e137

Upvotes: 2

Related Questions