1

I read that java treats float c = 5.6; as a double by default unless you specify the f suffix .

public class Main {
  public static void main(String[] args) {
    float c = 3.1e2;
    System.out.println(c);

  }
}

in this code 3.1e2 is a double and c should be also a double by default since i read that java will make every float variable a double by default unless you specify the f suffix . But i get an error saying Main.java:3: error: incompatible types: possible lossy conversion from double to float float c = 3.1e2; ^ 1 error

pcp pcp
  • 29
  • 3

5 Answers5

1

When you say " i read that java will make every float variable a double by default ", that is incorrect, you must have misread. Java will make floating point values double by default. If you write a literal value with a decimal point but without a d then it assumes you want a d. In your example 5.6 is a literal that is a double by default.

However, there is no default for typed variables, it is whatever type you give it. In your example c is declared as a float, so it's a float, meaning single precision floating point.

When you declare a variable to be type float you get enough room allocated to store a single precision floating point number in it. There isnt enough room to fit a double precision floating point number into it, and if you use a cast to force it, you will lose the extra precision from the double value. Which is what the error generated by the posted code is telling you, it's alerting you that you would lose data.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
1

To answer you're initial question,

"I read that java treats float c = 5.6; as a double by default unless you specify the f suffix ."

It treats the 5.6 as a double, not the entire assignment—c is still a float.

Here is an except from the Java Tutorial.
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

"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.

And, in terms of the follow-up question,

"... in this code 3.1e2 is a double and c should be also a double by default since i read that java will make every float variable a double by default unless you specify the f suffix ."

This is partially correct.  The c variable will not be implicitly converted to a double.
It will be whatever the declaring type is—in this case a float.

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)."

And, finally,

"But i get an error saying
Main.java:3: error: incompatible types: possible lossy conversion from double to float"

This is because the inferred cast, from one value to another, will reduce a plausible accuracy.

The Java Language Specification defines this operation as a, "narrowing primitive conversion".
Java Language Specification – Chapter 5. Conversions and Promotions.

"A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range."

Essentially, the conversion is not inferred when it is a narrowing procedure.
Thus, it throws a compiler error.

Reilas
  • 3,297
  • 2
  • 4
  • 17
0

The literal 3.1e2 is treated as a double, which is then assigned to the float variable c, which is where you get your error.

Aside from tacking on that f suffix to the literal, you can explicitly cast to float:

float c = (float)3.1e2;
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Chris
  • 26,361
  • 5
  • 21
  • 42
0

It is treating it as a double. However, you're assigning it to a float variable.

In some cases, C# and Java will allow implicit conversion; for example, the following is perfectly allowable:

class Main {
    public static void main(String[] args) {
        float c = 3.1e2f;
        System.out.println(c);
        
        double d = c;
        
        System.out.println(d);
    }
}

Since a double is a "larger" type than a float, there's no possibility of data loss if you do that.

However, if I try to do the following:

public class Main
{
    public static void main(String[] args) {
        double c = 3.1e2;
        System.out.println(c);
        
        float d = c;
        
        System.out.println(d);
    }
}

I end up getting the same compiler error you got. Again, that's because a double is a "larger" data type than a float, so the double could contain a value that's larger than the maximum possible value for a float. Consider the following code sample:

public class Main
{
    public static void main(String[] args) {
        double c = 10*(double)Float.MAX_VALUE;
        System.out.println(c);
    }
}

It prints 3.4028234663852886E39. Now, what would happen if I tried to put that in a float? Well, if I do the following, the result is Infinity:

public class Main
{
    public static void main(String[] args) {
        float c = 10*Float.MAX_VALUE;
        System.out.println(c);
    }
}

As you can see, the behaviors are quite different. If a conversion simply happened "in the background" without you being aware of it, you could get all kinds of unexpected results.

See also: Difference between implicit conversion and explicit conversion

0

If you just want a simple answer, without any explanation or anything, then your code will be as follows...

float c=3.1e2f;

System.out.println(c);

What you said is kinda partially incorrect, because in your statement, "I read that java treats float c = 5.6; as a double by default unless you specify the f suffix .", Java doesn't actually treat the c as double as well; c will be a floating point variable, but since a double value '5.6' is assigned to a float, and float is greater than double, thus the error.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 16 '23 at 10:04