[Fix] error: incompatible types: possible lossy conversion (Java)


Example 1 : incompatible types
public class JavaExamples {

    public static void main(String[] args) {
        byte b = false; //Incompatible type
    }

}

In Java, if you try to assign a value of a data type that is different than what the type expected by a variable the Java compiler will raise an "incompatible types" error. As you would see in the above example, we get this error when trying to assign a boolean type to a byte type.

byte b = true; //Compilation Error: incompatible types: boolean cannot be converted to byte
short s = false; //Compilation Error: incompatible types: boolean cannot be converted to short
char c = true; //Compilation Error: incompatible types: boolean cannot be converted to char
int i = false; //Compilation Error: incompatible types: boolean cannot be converted to int
long l = true; //Compilation Error: incompatible types: boolean cannot be converted to long
float f = false; //Compilation Error: incompatible types: boolean cannot be converted to float
double d = true; //Compilation Error: incompatible types: boolean cannot be converted to double


Example 2: incompatible types: possible lossy conversion
public class JavaExamples {

    public static void main(String[] args) {
        byte b = 300; //incompatible types: possible lossy conversion
    }

}

You make use of javac command to compile your code using the terminal, you will get the below compilation error,

JavaExamples.java:6: error: incompatible types: possible lossy conversion from int to byte
        byte b = 300;
                 ^
1 error

"incompatible types: possible lossy conversion" typically occurs when you try to assign a larger data type to a variable of a smaller data type.

As there is a risk of loss of data when you try to convert or save larger data into a smaller one, the compile will not let you compile code unless you fix the issue.

If you use an IDE for development, you will see the statement highlighted in red and when you hover over it, it would look something like this.

Incompatible types error in Idea IntelliJ
Data Type Size (bits) Value Range
byte 8 -128 to 127
short 16 -32,768 to 32,767
int 32 -2,147,483,648 to 2,147,483,647
float 32 1.4E-45 to 3.4028235E+38
long 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
double 64 4.9E-324 to 1.7976931348623157E+308
char 16 '\u0000' to '\uffff'

So, it is really important to know the size/range of each primitive datatype in Java as shown in the above table.

In the above example, we tried to assign a short value to a byte variable, so we encounter an "incompatible types: possible lossy conversion" error message because a byte can hold value larger values than the byte itself.

From Datatype To Datatype not possible
boolean byte, short, char, int, long, float, double
byte short, int, long, float, double
short int, long, float, double
char int, long, float, double
int long, float, double
long float, double
float double

The above table gives a list of all primitive datatypes and the equivalent list of incompatible data types that will cause the compilation error if you do so.

byte b1  = (short) 300; //incompatible types: possible lossy conversion from short to byte
byte b2  = 300; //incompatible types: possible lossy conversion from int to byte
byte b3 = 40000l; //incompatible types: possible lossy conversion from long to byte
byte b4 = 30.1f; //incompatible types: possible lossy conversion from float to byte
byte b5 = 300.15; //incompatible types: possible lossy conversion from double to byte

short s1  = 300111; //incompatible types: possible lossy conversion from int to short
short s2 = 40000l; //incompatible types: possible lossy conversion from long to short
short s3 = 30.1f; //incompatible types: possible lossy conversion from float to short
short s4 = 300.15; //incompatible types: possible lossy conversion from double to short

char c1  = 300111; //incompatible types: possible lossy conversion from int to char
char c2 = 40000l; //incompatible types: possible lossy conversion from long to char
char c3 = 30.1f; //incompatible types: possible lossy conversion from float to char
char c4 = 300.15; //incompatible types: possible lossy conversion from double to char

int i1 = 40000l; //incompatible types: possible lossy conversion from long to int
int i2 = 30.1f; //incompatible types: possible lossy conversion from float to int
int i3 = 300.15; //incompatible types: possible lossy conversion from double to int

long l1 = 30.1f; //incompatible types: possible lossy conversion from float to long
long l2 = 300.15; //incompatible types: possible lossy conversion from double to long

float f = 300.15; //incompatible types: possible lossy conversion from double to float

To fix this error, you have to either change the data type of the variable to match the data type of the value you are trying to assign, or you can perform an explicit type conversion - which is not recommended as there is a risk of data loss.

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















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