ERROR - Type mismatch: cannot convert from int to String

Error

Type mismatch: cannot convert from int to String


Error Type 

Compile Time


Sample Code 

public class Test {
static int element1;
String element2 = "Hello";

private void method1(){

element2 = 0;
}
}

Cause

String cannot be assigned a integer literal.

Resolution

Either we should assign element the string literal

public class Test {
private static int element1;
String element2 = "Hello";

private void method1(){

element2 = "0";
}
}

or 

change the data type of element.


public class Test {
private static int element1;
int element2;

private void method1(){

element2 = 0;
}
}
Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
comments powered by Disqus