Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

java - Floating point literal, floating literal, double Literal

Is the 'floating point literal' and the 'floating literal' the same thing in Java?

Also, is there anything called 'double literal' in Java?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"Floating point literal" refers to any literal of a floating-point type. Both float and double are floating-point types.

I have never come across the term "floating literal" before, but I would presume the intended meaning is the same as "floating point literal." You should make sure to ask for clarification though, because if the author intended to say "float literal" then that would be a literal of type float in particular, and not just any floating-point type in general.

As for the specific type of floating-point literals in Java, all literals of the general form #.# are double, unless you append f or F (same difference) to the literal to force it to be float instead.

Check out the tutorial page on Primitive Data Types by Oracle. I've copied the sub-section titled "Floating-Point Literals" below.

Floating-Point Literals

A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.

The floating point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).

double d1 = 123.4;
// same value as d1, but in scientific notation
double d2 = 1.234e2;
float f1  = 123.4f;

And as @Bubletan mentioned in comment to your question, if you're really curious you could check out the actual Java specification for float-point literals: §3.10.2. Floating-Point Literals

Examples of float literals:

1e1f    2.f    .3f    0f    3.14f    6.022137e+23f

Examples of double literals:

1e1    2.    .3    0.0    3.14    1e-9d    1e137

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...