17

if i'm not mistaken, "0.5" are decimal numbers; thus, making it a float value. but why is java telling me that it is a double?. the return statements are detected as errors by java, saying: "incompatible types:possible lossy conversion from double to float"

public float typeDmgMultiplr(String type,String type2){
        if(type.equalsIgnoreCase("grass")){
            if(type2.equalsIgnoreCase("grass"))
                return 0.5;
            else if(type2.equalsIgnoreCase("poison"))
                return 0.5;
            else if(type2.equalsIgnoreCase("fire"))
                return 0.5;
            else
                return 2.0;
        }
        else if(type.equalsIgnoreCase("fire")){
            if(type2.equalsIgnoreCase("grass"))
                return 2.0;
            else if(type2.equalsIgnoreCase("poison"))
                return 1.0;
            else if(type2.equalsIgnoreCase("fire"))
                return 0.5;
            else
                return 0.5;
        }
        else if(type.equalsIgnoreCase("water")){
            if(type2.equalsIgnoreCase("grass"))
                return 0.5;
            else if(type2.equalsIgnoreCase("poison"))
                return 1.0;
            else if(type2.equalsIgnoreCase("fire"))
                return 2.0;
            else
                return 0.5;
        }
        else{
            if(type2.equalsIgnoreCase("grass"))
                return 2.0;
            else if(type2.equalsIgnoreCase("poison"))
                return 0.5;
            else if(type2.equalsIgnoreCase("fire"))
                return 1.0;
            else
                return 1.0;
        }

    }
user3202989
  • 177
  • 1
  • 1
  • 3
  • 10
    `0.5` is by default `double` type. Append an `f` to make it a `float` - `0.5f` – Rohit Jain Feb 08 '14 at 12:07
  • 3
    A `double` is a floating point number with *double* precision, a `float` is a floating point number with *single* precision. If you just write `0.5` then it is interpreted as a `double`. To use the `float` type you have to write it as `0.5f`. – Jeroen Vannevel Feb 08 '14 at 12:08
  • 6
    Don't use `float` unless you have a very good reason. It'll just be an unnecessary headache most of the time. – user2357112 Feb 08 '14 at 12:21
  • 1
    And you will have even more headache when you migrate to Java-8 and discover that many cool new features don't support the `float` type at all. – Tagir Valeev Aug 12 '15 at 09:11

4 Answers4

12

If I'm not mistaken, 0.5 are decimal numbers; thus, making it a float value.

You should not rely solely on your intuition when learning a new programming language.

In fact, 0.5 is a double literal. For a float literal you need to write 0.5f.

As The Java Language Specification (JLS 3.10.2) states:

A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.

See also:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
7

You have two options.

One is change your method's return type to double.

The other is change the double values you are returning to float values, as many have said in the comments.

public float typeDmgMultiplr(String type,String type2){
    if(type.equalsIgnoreCase("grass")){
        if(type2.equalsIgnoreCase("grass"))
            return 0.5f;
        else if(type2.equalsIgnoreCase("poison"))
            return 0.5f;
        else if(type2.equalsIgnoreCase("fire"))
            return 0.5f;
        else
            return 2.0f;
    }
    else if(type.equalsIgnoreCase("fire")){
        if(type2.equalsIgnoreCase("grass"))
            return 2.0f;
        else if(type2.equalsIgnoreCase("poison"))
            return 1.0f;
        else if(type2.equalsIgnoreCase("fire"))
            return 0.5f;
        else
            return 0.5f;
    }
    else if(type.equalsIgnoreCase("water")){
        if(type2.equalsIgnoreCase("grass"))
            return 0.5f;
        else if(type2.equalsIgnoreCase("poison"))
            return 1.0f;
        else if(type2.equalsIgnoreCase("fire"))
            return 2.0f;
        else
            return 0.5f;
    }
    else{
        if(type2.equalsIgnoreCase("grass"))
            return 2.0f;
        else if(type2.equalsIgnoreCase("poison"))
            return 0.5f;
        else if(type2.equalsIgnoreCase("fire"))
            return 1.0f;
        else
            return 1.0f;
    }

}
pablosaraiva
  • 2,343
  • 1
  • 27
  • 38
3

In java, we have float and double as floating point literals, where double is the default data type of floating point literals and not the float. This is the reason why java is telling you that 0.5 is a double.

Possible conversion:

1) float a = (float) 0.5;

2) float a = 0.5f;

And reason for lossy conversion is because, double is bigger than float. When you try to fit-in the bigger one into the smaller one, you will get this error.

1

In java any decimal value without literal takes as Double.
So you have to mention the Float (F) behind the expression. E.g. Float f = 10.5F;

Hope this helps you. :)

ASHu2
  • 2,027
  • 15
  • 29