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

    Join Date
    Apr 2010
    Location
    Location : Location : Location
    Posts
    66
    Rep Power
    0

    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
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    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
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    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
    Location
    Norcross, GA (again)
    Posts
    1,804
    Rep Power
    1570
    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