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!
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
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- How to uninstall Microsoft Outlook on Mac - MacOS
- Understanding grep command and its usage [Unix/Linux/macOS/Windows-Bash] - HowTos
- What is Microsoft 365 Message You are using more licenses on your trial than what you will purchase once the free trial ends. - Microsoft
- Set Falling Show on Website for Christmas using Pure CSS Code - CSS
- How to place two div elements next to each other - CSS
- How to Preview HTML, CSS, JavaScript in Notepad++ - NotepadPlusPlus
- Google Search Console: HTTPS is invalid and might prevent it from being indexed - Google
- [Nopepad++] How to add text at end of each line - NotepadPlusPlus