简体   繁体   中英

Double instance variables in java

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

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.

double a = 10000000000; // ERROR! Integer number too large 
double b = 10000000000d; // OK!

没有d的数字0和26是int值。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM