September 28th, 2010, 07:37 AM
Java.lang.Character cannot be cast to java.lang.String
java.lang.Character cannot be cast to java.lang.String
what does it mean? which type of char cannot be cast to string? i was trying to cast maths operators (chars * / + -) to string, and i got that exception, cannot understand WHY!!!
September 28th, 2010, 07:50 AM
You can use the toString() method to convert a Character into a String.
You can't cast it because java.lang.Character doesn't inherit from java.lang.String.
September 28th, 2010, 10:00 AM
Another way to look at it.
A char is a primitive like an int or a byte.
A String is an object.
The char must be passed to a constructor of some kind to create a String object. Casting doesn't do that.
Comments on this post
September 28th, 2010, 01:54 PM
As Ishnid said, the solution in general is to use ToString(), which should work with any of the library classes in any case. However, this line puzzles me:
Just how were the operators represented (as chars or Characters, presumably), and what were you manipulating them for? Are you doing some sort of parsing of user input or of a source file of some nature?
Originally Posted by Dakaa