Comp Sci Java Error: "Type Mismatch" Explained

AI Thread Summary
The "Type mismatch: cannot convert from int to String" error occurs in Java when attempting to assign an integer value to a String variable without using quotes. In the provided code, the integers 8 should be enclosed in double quotes to be treated as String literals. This distinction is crucial, as only values in quotes are recognized as Strings, while unquoted numbers are treated as integers. Additionally, the discussion clarifies that character literals are enclosed in single quotes, while String literals require double quotes. Understanding these conventions is essential for avoiding type mismatch errors in Java programming.
Niaboc67
Messages
249
Reaction score
3
I am programming in Eclipse and I keep seeing the error "
Type mismatch: cannot convert from int to String", not only in the example I am going to provide but other programs. I've just started programming in Java. Could someone explain to me what this error exactly means?

publicclassPracOne{

publicstaticvoidmain(String[] args){String s1 = 8;

String s2 = 8;

System.out.print(s1.equals(s2));

}

}
 
Physics news on Phys.org
I rarely program in Java, but I suppose you would have to surround 8 by double quotes, on both lines. Otherwise the 8 represents an int(eger), not a string.
 
Please use [ code ] and [ /code ] tags as I have done below.
When you copy/pasted your code, it ended up with many spaces taken out that should be there. I have added them back in for the first couple of lines.
Niaboc67 said:
I am programming in Eclipse and I keep seeing the error "
Type mismatch: cannot convert from int to String", not only in the example I am going to provide but other programs. I've just started programming in Java. Could someone explain to me what this error exactly means?
Java:
public class PracOne{

   public static void main(String[] args)
   {
      String s1 = 8;
      String s2 = 8;
      System.out.print(s1.equals(s2));
   }
}
I agree with @Krylov. Your two String variables should be initialized with string literals, not int values. Those two lines should look like this:
Java:
String s1 = "8";
String s2 = "8";
 
Last edited:
Oh of course. Because only int can be a real number value, right? And when working with the String constructor it has to be characters or something within quotes?
 
Niaboc67 said:
Oh of course. Because only int can be a real number value, right?
Sort of. It can't have any fractional part.
Niaboc67 said:
And when working with the String constructor it has to be characters or something within quotes?
Yes
 
Is that always true @Mark44? why does the String Constructor have to be within quotes?
 
Niaboc67 said:
Is that always true @Mark44? why does the String Constructor have to be within quotes?
Is what always true?

A String literal has to be in double quotes. That's how you know it's a string literal. A character literal (such as 'a') is in single quotes. I don't understand what you're asking about a String Constructor. Your code doesn't explicitly use a constructor for the String class.
 

Similar threads

Replies
2
Views
1K
Replies
7
Views
3K
Replies
5
Views
3K
Replies
12
Views
2K
Replies
1
Views
2K
Replies
7
Views
2K
Replies
1
Views
2K
Back
Top