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.

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.
Have Questions? Post them here!
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Spring Boot CRUD Examples using JDBCTemplate - Java
- How to always show Bookmarks bar on Google Chrome Browser - Chrome
- Install Microsoft OneDrive on Mac - MacOS
- How to check if Java main thread is alive - Java
- Hide Navigation Bar from Android Screen Activity - Android
- How to make a Android button act as a toggle button - Android
- How to copy Chrome alert popup text to clipboard - Chrome
- Fix mySQL Error Cant connect to local MySQL server through socket /var/run/mysqld/mysqld.sock ERROR 2002 HY000 - MySQL