[fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics


Java incompatible types errors:
java: incompatible types: double cannot be converted to java.lang.Integer
java: incompatible types: double cannot be converted to java.lang.Integer
java: incompatible types: float cannot be converted to java.lang.Double
java: incompatible types: int cannot be converted to java.lang.Boolean
java: incompatible types: byte cannot be converted to java.lang.Character

One of the reasons for the incompatible types is that you are using generics with collections and passing a wrong type to a specific type,

Exmple:
1. public static void main(String[] args) {
2.   List<Integer> list = new ArrayList<>();
3.   list.add(10.0);
4. }

In the above example, we have a list of type Integer, and at line 3 we are trying to add a double to it, so you get a type safety compilation type error,

Required type: Integer
Provided: double

But if this happens at runtime you will get an error,

incompatible types double cannot be converted to java.lang.Integer
Fix:

For this example, if you expect the types for the list to hold doubles as well, change the type from Integer -> Double.

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap