Possible lossy conversion from double to float, given float values?

if I am not mistaken, "0.5" are decimal numbers; thus making it a floating value. but why does Java tell me it's double ?. return statements are detected as java errors 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; } } 
+11
source share
3 answers

You have two options.

One of them will change the type of the returned method to double.

Another is to change the double values ​​returned by 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; } } 
+5
source

If I'm not mistaken, 0.5 is decimal; thus making this value a floating point.

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

Actually, 0.5 is a double literal. For a literal with float you need to write 0.5f .

As stated in the Java language specification ( JLS 3.10.2 ):

A floating-point literal is of type float if the letter ASCII F or f added to it; otherwise, its type is double and you can add the suffix of the ASCII letter D or d .

See also:

+3
source

In Java, any decimal value without a literal takes the value Double .
So you should mention Float (F) after the expression. For example, Float f = 10.5F;

Hope this helps you. :)

0
source

All Articles