[2354] in java-interest
Re: more casting (sort of)
daemon@ATHENA.MIT.EDU (Thomas Ball)
Fri Sep 29 17:54:29 1995
Date: Fri, 29 Sep 1995 12:26:11 -0700
From: Thomas.Ball@Eng.Sun.COM (Thomas Ball)
To: lemay@lne.com
Cc: java-interest@java.Eng.Sun.COM
> So as I was playing with casting, I wanted to see how easy it was to
> convert an integer to a string and back again (part of my campaign
> for "useless examples." :)
>
> Integer to string isn't so bad:
>
> String str = String.valueOf(555);
>
> But I can't figure out how to convert a string back to an int.
Use the Integer class:
int x = new Integer(str).intValue();
> ... or a char.
Remember chars aren't tiny ints (as in C or C++), but instead
honest-to-gosh characters. I think you can cast between them, but
it's rarely a good idea because they serve a different purpose (use
ints for numbers, chars for possibly non-ASCII characters). If you
were parsing a string of digits, though, you could use the Character
class:
char c = str.charAt(index);
> or a boolean.
You have to write your own. Here's an example that should work
regardless of whether the Java implementation has been localized:
boolean b;
if (str.equalsIgnoreCase(new Boolean(true).toString()))
b = true;
else if (str.equalsIgnoreCase(new Boolean(false).toString()))
b = false;
else
throw InvalidYesNoStringException; // I made this exception up
Tom
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com