[Fix] java: incompatible types: java.lang.String cannot be converted to int


Example.java:6:21
java: incompatible types: java.lang.String cannot be converted to int

In Java you may get incompatible types compilation errors when you try to save incompatible type value in a specific data type which does not support it.

The error message "incompatible types: java.lang.String cannot be converted to int" occurs in when you try to assign a value of type String to a variable of type int, which is not possible because they are different data types.

Examples:
public class Example {
    public static void main(String[] args) {

        int myInt = "Harry";  //Required type: int Provided: String
    }
}
int myInt = "25"; //Required type: int Provided: String 
Java incompatible types String cannot be converted to int

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