views:

100

answers:

6

It is more stylistically acceptable in java to write constants in double operations with or without ".0" ?

As in,

double var= 200.0; double d= var/250.0; double h= 1.0 - d;

vs.

double var= 200; double d= var/250; double h= 1 - d;

Thanks

+1  A: 

I'm not a Java guy but C & C++ have the same issue. For me, I always use the ".0" just so that I'm never bitten by unwanted integer division. It also silences the occasional compiler warning about type conversion (though those are rare due to well-defined type promotion semantics from integers to floating point).

Drew Hall
A: 

it's better with .0

Itay
+4  A: 

It's not about style per se. When you write double var = 200;, 200 is an integer literal which is then converted into double via widening primitive conversion.

It's much better to be specific - if you're using a double constant, write it as double:

double d = 200.0;
double another = .01;
double exp = 1E3;

All of the above are floating point literals. Being specific is especially important in your second example:

var = 50;
double d = var / 250;

I've omitted the type of var on purpose. It's unclear what d will contain as a result - it could either be 0.2 or 0.0 (if var were integer). On the other hand, if you were to write double d = var / 250.0 the result would always be 0.2.

ChssPly76
I'm not sure, but I would *guess* that with a statement like `double var = 200;` the compiler is smart enough to treat that like `double var = 200.0;` That said, your point about integer division is the reason why you should always include the ".0".
Kip
I use IntelliJ and it warns you when you try to use double var = 200;. I think that declaring the literal in the correct type is always a good idea.
Ravi Wallau
@ChssPly76. The JLS is entirely clear about what the expressions mean. The uncertainty is in the mind of someone reading the code, and a good programmer will use a style that minimizes the uncertainty. This is DEFINITELY a style issue -- choosing a style that minimizes the chance that people will misunderstand what the code does.
Stephen C
@Stephen - indeed, and JLS clearly says that `200` is NOT a double; it's an integer literal. If you want to be precise, be precise to the end :-)
ChssPly76
@ChssPly76 - I don't understand your last statement. For example, in the original question `double h = 1 - d;` and `double h = 1.0 - d;` are equally precise when read in context. That's not the point. The point is about whether an average Joe programmer (with half of his mind on what he's going to do on the weekend) is going to understand the code correctly.
Stephen C
@Stephen - yes, they are; because it's subtraction. Would you be able to tell whether `double h = 1 / d` and `double h = 1.0 / d` are going to be equally precise without knowing the type of `d`? It stops being about style when you see someone defining constants like `final double ONE_HALF = 1/2` or `final long MS_IN_YEAR = 365 * 24 * 3600 * 1000` and then wondering where all the bugs came from.
ChssPly76
@ChssPhy76 - the type of `d` is double in the example. And if you are saying "what if you don't know that?", my response is that the compiler knows. As I said, the code in context is precise WHEN READ IN CONTEXT.
Stephen C
@Stephen - I'm really not quite sure what your point is anymore. Initializing double constants with integer literals is as much about "style" as not writing unit tests or exposing object fields as public. Can you do it and get away with it? Sure. If your definition of style is "anything goes so long as code compiles" I suppose you're right and it is a question of style.
ChssPly76
+1  A: 

Leading and trailing zeros as needed, with single precision constants tagged as such:

0.0 for a double.

0.0f for a float.

Boojum
That's not true, constants like ".1" and ".1f" are perfectly valid in C/C++/Java
Kip
@Kip: We're talking here about preferences, not what's strictly required.
Drew Hall
Sorry, I read "are needed", not "as needed". taking back the downvote...
Kip
+1  A: 

Most people will either do a "double take" when they see an arithmetic expression with mixed types, or completely misunderstand it. The same goes (to a lesser extent) for leading zeros; e.g. in 0.1 versus .1.

Good style is all about writing your code so that it is easy to understand, and easy for some else to maintain. So it follows that it is good style to use literals of the right type, rather than relying on promotion, and (to a lesser extent) to use leading zeros.

Stephen C
+1  A: 

As others have pointed out, it is just a question of preference and readability. See the following code and the disassembled bytecode:

public class Twohundred {
  public void intVersion() {
    double d = 200;
  }

  public void dblVersion() {
    double d = 200.0;
  }

}

result:

public class Twohundred extends java.lang.Object{

public void intVersion();
  Code:
   0:   ldc2_w #2; //double 200.0d
   3:   dstore_1
   4:   return

public void dblVersion();
  Code:
   0:   ldc2_w #2; //double 200.0d
   3:   dstore_1
   4:   return

}

As you can see, the two methods are as close to identical as you can get.

Fredrik