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;
Related
This question already has answers here:
why explicit type casting required from double to float but not from int to byte?
(4 answers)
Closed 1 year ago.
I'm not sure if my question is clear enough, so I will give examples. Let's think we have the next expression:
byte byteNumber = 10 * 10;
I understand the literal number 10 is an integer by default, so the expression 10 * 10 also results in an integer, BUT Java "demotes" it to a byte value since the variable (where the result is stored) is a byte.
However, why this works different?
int x = 10;
int y = 10;
byte byteNumber = x * y;
The line byte byteNumber = x * y; is marked as an error. I understand the expression x * y results in an integer but is not "demoted" as with the literals. Even if there is only one variable, like x * 10, the result won't be demoted. Why exactly? I believe it has something to do with the variables type, but literals are integers by default and they can get "demoted".
Another example I am struggling with is: we can assign integers literals to variables of type byte, short or char, and Java will automatically convert the integer into the type of variable we have declared, like:
short a = 10;
byte b = 12;
On the other hand, why can't we do something like this?
float c = 12.0;
Yes, 12.0 is a double, but why can't it be "demoted" to float and forces us to declare the literal as a float 12.0F? I understand this would represent a lose of information. However, converting an integer to a short or byte also represents a lose of information, isn't it?
Finally, why can we assign a integer literal to a variable of type short or byte...
short a = 10;
byte b = 12;
but we cannot pass an integer as argument to a method that expects a short/byte parameter?
public void newMethod(short x, byte y){
...
}
.
.
.
newMethod(10, 2)
It would be great if you could search some links where I can read this kind of stuff (since I'm not really sure how to search for these specific issues I have).
Thank you all in advance.
You could check the following two links out:
Why explicit type casting required from double to float but not from int to byte?
(the one I have already shared in a comment)
Implicit type cast not working for method parameters?
By the way, both of those questions were answered by the #1 StackOverflow contributor Jon Skeet :)
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
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
}
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
int totalOptCount = 500;
int totalRespCount=1500;
float percentage =(float)(totalOptCount/totalRespCount);
Why does this always return value 0.0? Also I want to format this into 00.00 format and convert into string?
Because the conversion to float happens after the division has been done. You need:
float percentage = ((float) totalOptCount) / totalRespCount;
You should be able to format using something like:
String str = String.format("%2.02f", percentage);
If you are using int values, using a double may be a better choice and have less rounding error. float can represent int values without error up to ~16 million. double can accurately represent all int values.
double percentage =(double) totalOptCount / totalRespCount;
Percentages are usually multiplied by 100, meaning you can drop the cast.
double percentage = 100.0 * totalOptCount / totalRespCount;
(totalOptCount/totalRespCount)
here both dividend and divisor are of type int which means they will allow only integer values and the answer of such equation will always be an integer literal.
if I break this it will be something like below
(double)(500/1500)
According to the actual calculation, 500/1500 will give you 0.33333 but compiler will convert this into integer literal because both operands are of type int
(double)(0)
Compiler gets an instruction to cast this 0 value to double so you got 0.0 as result
0.0
and then you can change the result to any format as suggeted by #Zach Janicki.
keep in mind if both the operands are of same type than result will be of same type too.
Integer division (which includes long, short, byte, char, int) in Java always returns an int (or long, if one of the parameters is long), rounding towards zero. Your conversion occurs after this calculation.
(The formatting question is already answered by the other answers - alternatively you could also have a look at java.text.NumberFormat, specially java.text.DecimalFormat.)
String.format("%2.02f", (float)totalOptCount/totalRespCount);
to format a double and print out as a percentage, you can use use
System.out.println(new DecimalFormat("##.##").format(yourDouble) + "%"));
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.