14

Came across the following expression on Java and I have no idea what "1d" means (r is an integer). There are also options for longs, doubles... What are they and what are they used for?

double y = r / (Integer.MAX_VALUE + 1d);

Thanks!

Community
  • 1
  • 1
aralar
  • 3,022
  • 7
  • 29
  • 44

1 Answers1

39

Sufix d after number means double, sufix f means float.

You can express double literal in eight ways: 1.0, 1d, 1D, 1.0d, 1.0D, 1., 1.d, 1.D and not truly double literal: (double)1. In the last example 1 is the literal, it is an int but then I cast it to double.

In your expression: double y = r / (Integer.MAX_VALUE + 1d); parentheses increase priority of expression so the (Integer.MAX_VALUE + 1d) will be evaluated first and because this is intValue + doubleValue the outcome will be of type double so r / (Integer.MAX_VALUE + 1d) will be also a double.

Yoda
  • 17,363
  • 67
  • 204
  • 344