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!
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 .
By writing 1.0f , you're telling Java that you intend the literal to be a float, while using 1.0d specifies that it should be a double. There's also L , which represents long (e.g., 1L is a long 1, as opposed to an int 1)
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With