Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating point literal, floating literal, double Literal

Tags:

java

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

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

like image 771
Shinjinee Maiti Avatar asked Dec 22 '16 18:12

Shinjinee Maiti


People also ask

What is a floating-point literal?

Floating-point literals are numbers that have a decimal point or an exponential part. They can be represented as: Real literals.


3 Answers

"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
like image 113
Travis Avatar answered Nov 14 '22 22:11

Travis


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.

like image 40
Eran Avatar answered Nov 14 '22 21:11

Eran


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

like image 44
A Sdi Avatar answered Nov 14 '22 21:11

A Sdi