Given the str reference which refers to an instance of String without
leading and trailing white space:
Convert String to int:
try {
int i = Integer.parseInt(str);
/* or */
Integer I = Integer.valueOf(str);
int i1 = I.intValue();
}
catch (NumberFormatException e){
;
}
Here is another example for the string presents a hexdecimal string:
try {
int i = Integer.parseInt(str, 16 );
}
catch (NumberFormatException e){
;
}
Note: Parses the string argument as a signed integer in
the radix specified by the second argument. The characters in the string must
all be digits, of the specified radix (as determined by whether Character.digit(char,
int) returns a nonnegative value) except that the first character
may be an ASCII minus sign '-' ('\u002D') to indicate
a negative value. The resulting byte value is returned.
Convert String to short:
try {
short s = Short.parseShort(str);
/* or */
Short S = Short.valueOf(str);
short s1 = S.shortValue();
}
catch (NumberFormatException e){
;
}
Convert String to byte:
try {
byte b = Byte.parseByte(str);
/* or */
Byte B = Byte.valueOf(str);
byte b1 = B.byteValue();
}
catch (NumberFormatException e){
;
}
Convert String to long:
try {
long l = Long.parseLong(str);
/* or */
Long L = Long.valueOf(str);
long l1 = L.longValue();
}
catch (NumberFormatException e){
;
}
Convert String to float:
try {
float f = Float.parseFloat(str);
float f1 = Float.valueOf(str).floatValue();
}
catch (NumberFormatException e){
;
}
Boolean B = Boolean.valueOf(str);
boolean b= B.booleanValue();
No exception needs to catch because the Boolean returned represents
the value true if the string argument is not null and
is equal, ignoring case, to the string "true".