1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Location : Location : Location
    Rep Power

    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!!!
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Shanghai, An tSín
    Rep Power
    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.
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Eastern Florida
    Rep Power
    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

    • spyder0101 disagrees : [0]No, a char is a primative, but a Character is an object.
  6. #4
  7. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Alpharetta, GA
    Rep Power
    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:
    Originally Posted by Dakaa
    i was trying to cast maths operators (chars * / + -) to string [...]
    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?
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in ShortUnderstanding the C/C++ Preprocessor
    Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

IMN logo majestic logo threadwatch logo seochat tools logo