I concur with the above post. Incidentally, a lot of people are surprised when they try to add/subtract/multiply/divide two literals and store it in a long and it doesn't work:
This will not compile because literals with decimal points are declared as doubles by the JVM. So, there are two fixes, either of which will work:
The first way is preferable, especially if you're performing a long
string of calculations because it will carry out all of those calculations to the level of precision allowed by doubles, only rounding off precision at the last moment before assigning the result to x. In the second way, each operand is cast to a long and loses the precision of the double, so the loss-of-precision errors can compound and end up throwing off the end value. (This is usually only significant if you're working with a very long list, or at the limits of long precision--in which case you ought to be using doubles anyway.)
Another thing that always shocks people is when they run this code the first time:
You'd think the result would be 1.5. It's not...it's 1.0. That's because when you use literals without decimal points, the compiler assumes they're ints. Dividing two ints yields an int result--any fractional portion is simply dropped, so 1.5 becomes 1. As long as one or both of the operands is marked as a double (or a float, but that would be silly seeing as how this code stores the result into a double anyway), things work out.
As it happens there's lots of ways to indicate this:
The last option of each block is the most readable of that block (I think), so therefore the best way. When it comes to appending a type suffix to literals, I usually use small case D's for doubles because a capital D can look a lot like a zero if you're not paying attention. Along the same vein, to indicate a long literal I usually will use capital L instead of small l because a small l looks a lot like a one. Either case of B and F work (for bytes and floats, respectively), while I only would use small i to denote an integer (again, capital I looks like 1).
That's if I'm using those literal suffixes--because of the rules governing these primitive types, there is rarely if ever a need to specify bytes, ints, or doubles with these suffixes so I have only ever found occasion to append L's and d's, and I prefer using ".0" instead of "d" anyway, so I really only ever use "L".
Hey, I didn't say this post would be fascinating.
sev