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.
Facing issues? Have Questions? Post them here! I am happy to answer!
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
- Notepad++ Ghost typing Examples - NotepadPlusPlus
- Vertical align two div's in Bootstrap [HTML CSS] - Bootstrap
- How to Calculate Age from Date of Birth in Java - Java
- Eclipse : This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in - Eclipse
- How to know if someone has read your WhatsApp message - WhatsApp
- Convert java.time.LocalTime to java.util.Date Object - Java
- Convert GMT/UTC timezone in Excel to EST, CST, MST, PST, AST and HST - Microsoft
- Calculate Area of a Rectangle - C-Program