You can convert strings into numbers using the Integer, Float, Double and Long type wrapper classes as indicated by the following code snippet:


class ConvertTest {

  public static void main (String args[]) {

    String str;
    str = "25";
    int i = Integer.valueOf(str).intValue();
    long l = Long.valueOf(str).longValue();

    str = "25.6";
    float f = Float.valueOf(str).floatValue();
    double d = Double.valueOf(str).doubleValue();  

There are no equivalent Short and Byte classes in Java 1.0. There are in Java 1.1. For shorts and bytes use the Integer class but use the byteValue() or shortValue() methods instead.

Posted in: Java

Related FAQ's

Marius Ion ANGEL HOT SOFT LLC (800) 316-7677