1

Why do double instance variables in java have a lower case d attached to them? Do they need to have this?

Example:

double area = 0d;
double avgDailyTemp = 26d;

etc...

Taylor Marie
  • 357
  • 1
  • 15

4 Answers4

4

A number literal by default is an integer. If you attempt to pass a number like ten billion into Java double it'll error since it's outside the bounds of an integer. Specifying the lowercase d explicitly defines it as a double literal instead.

Alex Popov
  • 2,509
  • 1
  • 19
  • 30
1
double a = 10000000000; // ERROR! Integer number too large 
double b = 10000000000d; // OK!
MariuszS
  • 30,646
  • 12
  • 114
  • 155
0

Without the d the numbers 0 and 26 are int values.

Michael Welch
  • 1,754
  • 2
  • 19
  • 32
0

It is not the variable which requires the d, but the constant value declared. It is a "hint" to the compiler of the data type.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Brett Okken
  • 6,210
  • 1
  • 19
  • 25