Java Error: "Type Mismatch" Explained

Click For Summary

Discussion Overview

The discussion revolves around a Java programming error message: "Type mismatch: cannot convert from int to String." Participants explore the meaning of this error, particularly in the context of initializing String variables with integer values instead of string literals.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant seeks clarification on the error message encountered when trying to assign integer values to String variables.
  • Another participant suggests that the integer should be enclosed in double quotes to be treated as a String.
  • A third participant agrees with the previous suggestion and emphasizes the need for string literals instead of integers.
  • Some participants discuss the nature of integers and strings, noting that integers cannot have fractional parts and that string literals must be enclosed in quotes.
  • Questions arise regarding the necessity of quotes for string literals and the distinction between string and character literals.
  • One participant expresses confusion about the String constructor and its requirements for quotes.

Areas of Agreement / Disagreement

Participants generally agree that string literals must be enclosed in double quotes, but there is some confusion regarding the specifics of the String constructor and the definitions of string versus character literals. The discussion remains unresolved on certain points, particularly regarding the String constructor.

Contextual Notes

There are limitations in understanding the requirements for string initialization and the distinction between different types of literals. Some assumptions about the nature of integers and strings are not fully explored.

Who May Find This Useful

New Java programmers, individuals encountering type mismatch errors, and those interested in understanding string initialization in Java.

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 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K