## Why will Java convert the double to float type in this situation?

Today, I defined two float variable ** f1** and

**. Then I perform an addition, "+", arithmetic operation and assign to float variable**

*f2***.**

*f*float f1 = 0.5048076923076923F; float f2 = 0.5048076923076923F; float f = f1 + f2;

This is the output:

According to this picture, All floating point values (** float** and

**) in an arithmetic operation (**

*double***+, −, *, /**) are converted to

**double**type: picture source: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/mixed.html

I found an identical question but it hasn't explain why. Why doesn't eclipse have any issue tips? Is it the reason why the value of ** "f1 + f2"** is a float type? And, why will Java auto convert the double to float type if like the above picture saying?

PS: English isn't my Mother tongue, please forgive me if this question has somes grammar issue. Thanks. :)

**Double floatValue() in Java with Examples,** The floatValue() method of Double class is a built in method to return the value specified by the calling object as float after type casting. Syntax Otherwise, both operands are converted to type int. As you can see, floats are only converted to double if the other operand is a double, not in all cases as your syllabus incorrectly states. That is, the sum of two floats is a float, but the sum of a float and a double is a double.

**How to convert String to Float in Java and vice-versa,** There are three ways to convert a String to float primitive in Java parseFloat(), data types in Java, you will often find yourself converting String to Int, Double,� The java.lang.Double.floatValue() method returns the float value of this Double object. Declaration. Following is the declaration for java.lang.Double.floatValue() method. public float floatValue() Parameters. NA. Return Value. This method returns the double value represented by this object converted to type float. Exception. NA. Example

Hi for floating points values JVM uses instructions like this:

'+' : fadd : pops two floats, adds them, and pushes the float result '-' : fsub : pops two floats, subtracts them and pushes the flot result

similarly for there are other instructions set for other operators like * and / etc.

So as per JVM implementation point of view float + float will result to float. But float + double will result to double.

For more information, you can read chapter 14. Floating-Point Arithmetic of the book "Inside Java Virtual Machine"

**Java – Convert int to double example,** In this java tutorial, we will see how to convert int to double in Java. Since double has longer range than int data type, java automatically converts int. The double data type is more precise than float in Java. By default, floating point numbers are double in Java. In order to store them into float variable, you need to cast them explicitly or suffix with ‘f’ or ‘F’.

What the other answers are missing is the hardware level. Floating point processors, in general, convert all operations to double (or quad), and then return the result in the requested precision. The Java specification may not recognize the conversion, but if it uses the floating point hardware then the conversion happens.

**Java - Convert double to int example,** Java – double to int conversion using type casting. To typecast a double value to integer, we mention int keyword in the brackets before the decimal double value. The first URL I linked to above shares several other good rules. This first one is about float and double values: All floating point values (float and double) in an arithmetic operation (+, −, *, /) are converted to double type before the arithmetic operation in performed. This next rule is about using integer values in arithmetic operations:

**Chapter 5. Conversions and Contexts,** A conversion from type double to type long requires a non-trivial translation from a The conversions possible in the Java programming language are grouped into In this case, the resulting floating-point value will be a correctly rounded� In Java, it corresponds to the possibility of losing the value or precision of a variable while converting one type to another. When we try to assign a variable of large-sized type to a smaller sized type , Java will generate an error, incompatible types: possible lossy conversion , while compiling the code.

**NUM12-J. Ensure conversions of numeric types to ,** Java provides 22 possible narrowing primitive conversions. Conversion from int or long to float or from long to double can lead to loss of precision (loss of least correctly reflect the intent of the programmer is infeasible in the general case. If neither is a double but one is a float, the other is converted to a float. If neither is a double nor a float but one is a long, the other is converted to a long. If all else fails, both values are converted to int. Type casting. Casting is similar to conversion but isn’t done automatically. If you want to convert a double to an int, for

**“mismatch cannot convert from double to float” in Java?,** I will explain this to you in simple language. What is a “double” and a “float” in java? They are data types right. And we know that a data type is a container that� In this article, we will show you how to convert Java String to wrapper Float class or primitive type float code examples. There are different ways we can convert Java String to wrapper Float class or primitive type float. This is a common scenario to convert String to wrapper Float class or primitive type float in java programming language.

##### Comments

- The result of adding two
`float`

(s) is a`float`

. As far as I know,`float`

(s) are**not**widened to`double`

before arithmetic. That assertion doesn't make any sense. - Might be he just copy-pasted a text for double and forgot to revise it.
- Maybe my expression is not very clear, but Stephen C's answer has solved this problem. What I thought before was that two floats would be converted to a double type. Thank you for your help. :D
- Thank you very much for your answer. This answer solved my problem very well. I am going to tell my teacher. 😀
- .... unless you use
`strictfp`

. Besides, those conversions are not what the JLS means when it uses the term "conversion". And the fact remains that the OP's teacher is saying (with no qualification) that the computation is always performed using double arithmetic ... which in the light of what you say ... is wrong for**two**reasons.