Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: what is 1d?

Tags:

java

double

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!

like image 757
aralar Avatar asked Dec 08 '14 22:12

aralar


People also ask

What does 1D mean in Java?

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 .

What does the D mean after a number Java?

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)


1 Answers

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.

like image 52
Yoda Avatar answered Oct 07 '22 22:10

Yoda