简体   繁体   中英

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?

"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 

Java has both float literals marked with a f suffix (for example, 2.5f) and double literals, which don't require a suffix (for example 2.5).

The difference between the two can be demonstrated when you attempt to assign such literals to variables :

double dl = 2.5; // passes compilation
float fl1 = 2.5; // error - "Type mismatch: cannot convert from double to float"
float fl2 = 2.5f; // passes compilation

Both can be considered as floating point literals, since both double and float are floating point types.

"floating-point literal" will be float type if it ends with F or f ; otherwise it is a double

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