Java Reference
In-Depth Information
ponding wrapper class. Each parsing method has its own rules about
the allowed format of the string, for example Float.parseFloat will accept
a floating-point literal of the form "3.14f" , whereas Long.parseLong will
not accept the string "25L" . These numeric parsing methods have two
overloaded forms: one that takes a numeric base between 2 and 32 in
addition to the string to parse; and one that takes only the string and
assumes base 10. These parsing methods will also reject the string if it
has characters representing the base of the number, such as "0x12FE"
for a hexadecimal value, or "\033" for an octal value. However, the In-
teger and Long wrapper classes also provide a static decode method that
will parse a string that does include this base information. For the nu-
meric types, if the string does not represent a valid value of that type,
a NumberFormatException is thrown.
To convert a String to a char you simply extract the first char from the
String .
Your classes can support string encoding and decoding by having an ap-
propriate toString method and a constructor that creates a new object
given the string description. The method String.valueOf(Objectobj) is
defined to return either "null" (if obj is null ) or the result of obj.toString .
The String class provides enough overloads of valueOf that you can con-
vert any value of any type to a String by invoking valueOf .
13.2.6. Strings and char Arrays
A String maps to an array of char and vice versa. You often want to build
a string in a char array and then create a String object from the con-
tents. Assuming that the writable StringBuilder class (described later)
isn't adequate, several String methods and constructors help you con-
vert a String to an array of char , or convert an array of char to a String .
There are two constructors for creating a String from a char array:
public String(char[] chars, int start, int count)
 
Search WWH ::




Custom Search