• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

End your double value with 'd'

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Members,


Is it a good programmer practice to end your double value with 'd' in your Java program?

I have learnt that it is optional. However, I would like to know if it is a good practice to do so.

Example:

double value = 11.99d;


Thank you.
 
Master Rancher
Posts: 172
7
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code that I have come across, I almost never see it specified.
 
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never seen 'd' used in production level code. It just gives the reader's brain one more thing to parse and slows down the reading process.
 
Saloon Keeper
Posts: 3443
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed.

There are some methods in the Java library that force you to use floats, and that means putting an 'f' to your values. Tedious!
 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . methods in the Java library that force you to use floats . . .

. . .  or a (float)... cast. It is more than tedious, it is bad programming because whoever wrote that still thought they were writing C. I think you shouldn't use floats full stop.
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rajan Masi wrote:. . . Is it a good programmer practice to end your double value with 'd' . . . ? . . ..

As a general rule:- No. I don't like to write 1.234d. You will occasionally see integers converted to doubles with a d, e.g. 1234d, but it is probably better to write 1234.0. And I don't use a terminal f because I don't like to use floats at all.
 
Piet Souris
Saloon Keeper
Posts: 3443
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:(...) I think you shouldn't use floats full stop.


As a simple example: there are handy methods in the Color class that use floats, and I wil remain using them.
 
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think personal preference is a good enough reason to recommend or discourage a practice. Carey's tack is a better way to think about it: if it gets in the way of someone reading the code, don't do it. Otherwise, if it helps clarify your intent, then go ahead. There might also be very specific technical reasons for using float over double. Try searching for "Why use float instead of double?"

Java developers should know the default primitive types for numeric literals: double and int. So, a declaration like double value = 11.99d is redundant because 1) You already know the intent is to have a double value based on the declared type and 2) The 11.99 is already a double by default. As Carey pointed out, adding 'd' to a literal like that is not a common practice and doing so will just make most people reading the code do a double take (pardon the pun).

For the above reasons, I would recommend following the Rule of Thumb "Use double unless you have a good reason to use float (and don't add redundant 'd' to double literal values)."
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why weren't those methods written to use proper numbers, like doubles? Who wants to write new Color(0.3f, 0.4f, 0.5f) when they could write new Color(0.3, 0.4, 0.5)? The floating‑point numbers are simply converted to ints in the range 0...0xffinc anyway.

Come to think of it, ints in that range are unsigned bytes. Now, that is even more controversial
 
Rajan Masi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies Members
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!