Developer
Developer

Reputation: 81

How to decide whether the given value is float or double in Java?

What is the data type for the number 9.6352 in Java?

Actually if I postfix the number with 'd' or 'f'. then it will be quite easy to decide. But if I leave it as alone, then which will be the default type of the number in java?

Upvotes: 5

Views: 1124

Answers (4)

Mat
Mat

Reputation: 206679

Go check out the JLS section on floating point literals (§3.10.2):

A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.

So it's a double if it's not suffixed.

Upvotes: 10

Peter Lawrey
Peter Lawrey

Reputation: 533482

For you interest;

  • 0xD is 13, 0xF is 15
  • 0D is (double) 0 and 0F is (float) 0.

Upvotes: 2

nitin1706
nitin1706

Reputation: 395

By default, any floating point data type without 'f' or 'F' postfixed, is considered as of type double. Since the given number doesn't end with 'f'/'F', it is of type double.

We can optionally add 'd' or 'D' at the end to mark it as a double type, but again, it is optional.

Upvotes: 8

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

By default it's double primitive type.

Upvotes: 1

Related Questions