5

I did some googleling, but was unable to find a solution to my question.

Are there generally accepted java coding conventions according to double and float declaration?

What syntax is recommended by those guidelines?

      Double d = 1d;
      Double d = 1D;
      Double d = 1.;
      Double d = 1.0;
      Double d = 1.0d;
      Double d = 1.0D;

Same goes for Float and Long and also their primitives.

      Long l = 1l;
      Long l = 1L;

All compile the same, but there are difference in clearness when reading these lines of code. The one with the upper case d seems to be more likely to be read wrong --> "D" could be read as "0" or so.

Datz
  • 3,156
  • 3
  • 22
  • 50
L.Butz
  • 2,466
  • 25
  • 44
  • 3
    i always use lowercase letters. `float f = 3,14f; double d = 3,14d; long l = 10L` but for long its better to use an uppercase L, because the lowercase looks like 1. – kai May 09 '14 at 06:31
  • I request this question to be reopened. I think it's a perfectly legitimate question about coding standards that can be answered objectively. It is not opinion-based. – Datz Jan 22 '21 at 10:38
  • I would like to answer this question with a list of accepted coding standards (SUN, Google, Gitlab, MISRA) and classify/evaluate them. But the question is closed. In short: many coding conventions suggest to use `L` but not `l`. Some prefer upper case letters but there are discussions that `D` is worse than `d`. And none of them tell if you should use `1.` over `1.0` over `1d`. – Datz Jan 22 '21 at 10:44

3 Answers3

12

It's really just personal preference and I believe lowercase is more common. It's like indentation, pick something and stick with it.

The Java language spec mentions that captial L is preferred for long values because it's easier to read but it doesn't say anything about f/F or d/D.

takteek
  • 7,020
  • 2
  • 39
  • 70
5

You usually use the letter that seperates best from the numbers. I didn't know the reason behind this for quite some time, but I was told that capital letters are more easily seperatable from numbers. imagine you have an "L" for Long:

long x = 10000L

is much clearer than

long x = 10000l

The "l" could be easily mistaken to be a "1".

In your case, you should prefer "d" over "D".

PKlumpp
  • 4,913
  • 8
  • 36
  • 64
  • 1
    what you say is true for L/l, but for D/d the OP claims the exact opposite... – chiccodoro May 09 '14 at 06:36
  • Indeed; 'L' gets an explicit mention in Google's public [Java Style guide](https://google.github.io/styleguide/javaguide.html#s4.8.8-numeric-literals) (§4.8.8 numeric literals) – Luke Usherwood Sep 10 '19 at 07:20
5

From the Java Tutorials:

Floating-Point Literals

A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.

The floating point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).

double d1 = 123.4;
// same value as d1, but in scientific notation
double d2 = 1.234e2;
float f1  = 123.4f;

This tells you that:

  • Both D and d have the same meaning - just as you observed
  • "By convention [D or d are] omitted" because they are the default

Questions of the type "what is better" are offtopic on StackOverflow since they can't have a correct or incorrect answer, only opinions.

But if you ask for a convention I would tend to follow what I've found above - omit the suffix.

Community
  • 1
  • 1
chiccodoro
  • 14,407
  • 19
  • 87
  • 130