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...

4
  • See this answer: stackoverflow.com/a/28353219/1057429 it talks about long but provides information about double as well Commented Feb 7, 2015 at 22:20
  • Can you reword your question, because its unclear. whats a double instance variable? double is primitive and not an object, primitives are lower cased in Java Commented Feb 7, 2015 at 22:22
  • @SleimanJneidi OP is asking if d part in 26d is mandatory for double. Commented Feb 7, 2015 at 22:30
  • I see, it doesn't need to be lower case, 26D works as well Commented Feb 7, 2015 at 22:32

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You can also add a "." at the end instead of d: double area = 260000000000.;
1
double a = 10000000000; // ERROR! Integer number too large 
double b = 10000000000d; // OK!

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.