Why floating point number 333.50 is double [duplicate] - java

This question already has answers here:
Java: Why do you need to specify an 'f' in a float literal? [duplicate]
(3 answers)
Declaring floats, why default type double?
(3 answers)
Closed 5 years ago.
Why in this line the floating point number is treated as double? I thought float has enough space to hold that number.
float f = (float)333.50;

The value 333.50 can be represented in the float type but is a double literal. Use 333.50F to have a float literal.
The specification, see Oracle Java documentation about Primitive Data Types, is clear about the different literals:
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.
There is a similiar case for integer literals but here the smaller type (int) is the default:
Integer Literals
An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. It is recommended that you use the upper case letter L because the lower case letter l is hard to distinguish from the digit 1.

Because the Java specification says that by default a floating-point literal is a double value.
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.
Rather than using a narrowing conversion (double to float), just use the F prefix in the literal declaration :
float f = 333.50F;

333.50 is a literal of type double. That's simply the rule - it comes from C way back in the 1970s. It's just like 0 is a literal of type int.
The equivalent float literal is 333.5f, or you can use a compile time evaluable constant expression like (float)333.50.
Indeed 333.5 can be represented exactly as a float as it's a dyadic rational.

by default floating point literal is double in java , if you want the value to float you may do this
float f = 333.5f

This is how you define literals in java (JLS)
int a = 12;
long b = 12L;
float c = 12.0f;
double d = 12.0d;
so you can write those like;
i = 1222;
is an integer literal
and the same way
j = 3.1415;
is a double literal
of course you can EXPLICITLY define the type:
i = 1222L; //for long values
j = 3.1415f; //for float values

Java by default treats any decimal point values as the double. so you need to particularly define which type of primitive data type you are using.
for example:
`int i=10; // default
long l=123456L;
float f=123.12F;
double d=123.12345; // default
`
so you must explicitly define the what type of data type you are using when not using the default types

Related

What's the difference between declaring a long variable with 'L' and without? [duplicate]

This question already has answers here:
Using the letter L in long variable declaration
(2 answers)
Closed 5 years ago.
What's the difference between the following?
long var1 = 2147483647L;
long var2 = 2147483648;
(or any primitive variable declaration)
Does it have any performance issue with or without L? Is it mandatory?
In the first case you are assigning a long literal to a long variable (the L or l suffix indicates long type).
In the second case you are assigning an int literal (that's the default type when no suffix is supplied) to a long variable (which causes an automatic type cast from int to long), which means you are restricted to the range from Integer.MIN_VALUE to Integer.MAX_VALUE (-2147483648 to 2147483647).
That's the reason why
long var2 = 2147483648;
doesn't pass compilation (2147483648 is larger than Integer.MAX_VALUE).
On the other hand
long var2 = 2147483648L;
would pass compilation.
For easy understanding each of the type have range in java.
By default every digit you entered in java is either byte or short or integer.
short s = 32767;
byte b = 127;
int i = 2147483647;
So if you assign anything except from their range you'll get compilation error.
int i = 2147483648; //compilation error.
Type range
And when you write long longNumber = 2147483647;
though it falls in long range but internally java treat it as
long l = (int) 2147483647;
you wont get any errors.
But if we assign beyond the range of integer like
longNumber = 2147483648; we will get compilation error as
long o = (int) 2147483648;
here java will try to convert the 2147483648 to int but it is not in int range so widening error is thrown.
To indicate java that the number what we have written is beyond the integer range just append l or L to the end of the number.
so java will wide his range till long and convert it as
long o = (long) 2147483648;
By default every floating point or digit with floating points (.) are size of double. So when you write some digits with (.) java treat as a double and it must be in double range.
As we know the float range is smaller then double.
so when you write
float f = 3.14;
though it falls in double range but internally java treat this assignment as
float f = (double) 3.14;
here you are assigning the double to float narrowing which is not correct.
so either you have to convert the expression like that
float f = (float)3.14;
or
float f = 3.14f; // tell jvm to assign this in float range by appending **f** or **F**
If we don't mention the L with the value then value is considered to be int value.
It type casts the int to long automatically.

Literal Assignment in Java [duplicate]

This question already has answers here:
Java's L number (long) specification
(7 answers)
Closed 6 years ago.
what's the difference in defining
double example = 23.1d
or
double example = 23.1
Why long, float and double can end with l, f, d?
There is no difference between double example = 23.1d; and double example = 23.1; because a floating point literal without a type suffix is always interpreted as a double.
The type suffixes are necessary in order to avoid ambiguities in certain scenarios.
For example, java supports method overloading. This means that you can have void x( float f ); and void x( double d ); Both methods are called x; which one will be selected depends on the type that you pass; if you pass a variable which is already known to be either float or double, things are clear; but if you want to pass a literal, like this: x( 5 ); then you have to be able to specify whether you mean this 5 to be a float or a double, so as to select the right method.
There are a few other very nuanced situations where the type of the literal matters. For example, the following code:
System.out.println( "" + (2/3.3333) );
System.out.println( "" + (2/3.3333f) );
Yields the following output:
0.6000060000600006
0.600006
...because the first number is a double, while the second number is a float.
Similar disambiguation concerns make the "L" type suffix necessary for long integer literals.
23.1d (or just 23.1, since double is the default) is a different number than 23.1f. Since 23.1 cannot be exactly represented, 23.1f is the closest float to 23.1, and has only about 6 significant figures. As a double, 23.1 will have about 16 significant figures and can therefore get a bit closer to the actual value.
Note that
double example = 23.1f;
is equivalent to
float f = 23.1f;
double example = (double)f;

Why does type double PI = 22/7 returns 3, but double PI = 22.0/7 or 22/7.0 returns correct result 3.14..? [duplicate]

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 8 years ago.
I tried of finding answer by googling as well as debugging variable behavior, but unfortunately I dint find any proper answer. Its a question related to Java for instance.
Question :
Why 'double' type variable behaves like 'int' type in below condition :
double PI = 22/7 which returns 3
but, double PI = 22.0/7 or 22/7.0 returns 3.14xxxxxxx ?
Help appreciated...Thanks.
Because that's how Java (and some other programming languages) have been implemented.
Since both are integers, the expected result will be an integer as well:
int/int = int
In the other hand, when one operator is double, the result will also be double
double/int = double
int/double = double
Because in java arithmetic operations output gives the result in terms of highest data type among all involved variables or constants(Applied only on primitive data types).
For example if in any arithmetic operation like below:
Scenario 1=> var1 is int, var 2 is float, var3 is double: You will get result in double
Scenario 2=> var1 is short, var2 is long and var3 is float: you will get result in float
Scenario 3=> var1 is int, var2 is long, var3 is double: you will get result in double
Note: float family data type(float & double) always dominate to int family data types even both have same size, like in case of long and float output will be in float.
Because by default in java numerals are integer data type, so, when you are doing numeric operation with integers, the result also will be integer. When you assign that integer to a double variable, it is promoted to a double, but its value is kept. So you end up with a double with the exact same value as the result integer -- in this case, 3.0.
In your first case, both are integers, so the result also an integer, and you have assigned to double. But the conversion(integer to double) happened before assignment to double.
In the second or third case, one in double, so the operation done on double, So the result also a double value.
Or 'Pi= 22D / 7D' does it too. Here 22 and 7 are declared as 'double', not 'int'.
public static void main(String[] args) {
double d = 22 / 7; // same as double d = (int)22 / (int) 7
System.out.println(d); // so prints 3.0
double dd = 22.0/7; // same as double dd = (double)22 / (int) 7
System.out.println(dd);//prints 3.14xxxx
}

What is the purpose of having a suffix for Java Primitive Data Types?

Sorry for a beginners question! I am learning Java and I'm a bit confused on one topic.
For example, I am declaring and initializing a variable as a float such as:
float myVariable = 9.5;
In some tutorials online, I see floats being declared and initalized like that, but also with a suffix such as this:
float myVariable = 9.5F;
What is the purpose of the the suffix if I am already saying "float myVariable"?
Thanks!
Floating point literals are by default of double type. So, 9.5 is double not float. To make it float we append an f at the end.
So, in first case:
float myVariable = 9.5;
You are trying to assign a double type literal to a float type variable, which requires an explicit typecasting. The statement won't compile. You need to add a cast there:
float myVariable = (float) 9.5;
While in 2nd case:
float myVariable = 9.5F;
there is no conversion.
Java uses suffixes with these primitives types, here is a brief intro:
float
float fVariable = 5.5 is by default double while float fVariable = 5.5f makes it float
long
long lVariable = 55 here literal 55 is by default int type but long lVariable = 55l makes it long type.
double
double dVariable = 5.5 and double dVariable = 5.5d are similar but here d suffix is optional
Have you tried to compile your code?
This will not compile, because by default all floating-point literals are doubles
float myVariable = 9.5;
You can use explicit cast operation
float myVariable = (float) 9.5;
But it's too verbose, so thankfully the 'F' suffix was introduced:
float myVariable = 9.5F;
BTW, both UPPER ('F') and lower ('f') cases are allowed.
Thought it might be worth providing an answer that cites the specification. From ยง3.10.2 Floating-Point Literals:
A floating-point literal is of type float if it is suffixed with an
ASCII letter F or f; otherwise its type is double...
In other words, 9.5 and 9.5f are two distinct literals with different types.
9.5 is of type double and cannot be assigned to a float without
explicit casting
9.5f is of type float (and as such can obviously be
assigned to a variable of type float)

What do F and D mean at the end of numeric literals?

I've seen some of this symbols, but I cannot find anything strange with it,
double d = 5D;
float f = 3.0F;
What does the D and F behind 5 exactly means?
Means that these numbers are doubles and floats, respectively. Assume you have
void foo(int x);
void foo(float x);
void foo(double x);
and then you call
foo(5)
the compiler might be stumped. That's why you can say 5, 5f, or 5.0 to specify the type.
D stands for double
F for float
you can read up on the basic primitive types of java here
http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
I would like to point out that writing
5.1D or 5.1 : if you don't specify a type letter for a comma number then by default it is double
5 : without the period, by default it is an int
They're format specifiers for float and double literals. When you write 1.0, it's ambiguous as to whether you intend the literal to be a float or double. By writing 1.0f, you're telling Java that you intend the literal to be a float, while using 1.0d specifies that it should be a double. There's also L, which represents long (e.g., 1L is a long 1, as opposed to an int 1)
D stands for double and F stands for float. You will occasionally need to add these modifiers, as 5 is considered an integer in this case, and 3.0 is a double.
As others have mentioned they are the Type definitions, however you will less likely see i or d mentioned as these are the defaults.
float myfloat = 0.5;
will error as the 0.5 is a double as default and you cannot autobox down from double to float (64 -> 32 bits) but
double mydouble = 0.5;
will have no problem
It defines the datatype for the constants 5 and 3.0.

Categories

Resources