The Java Course provides a general introduction to programming in Java. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.

Getting the Values Out

Java knows how to print wrapper objects, so the easiest way to extract a value is just to print the object:

    Integer i = new Integer (17);
    Double d = new Double (3.14159);
    System.out.println (i);
    System.out.println (d);

Alternatively, you can use the toString method to convert the contents of the wrapper object to a String

    String istring = i.toString();
    String dstring = d.toString();

Finally, if you just want to extract the primitive value from the object, there is an object method in each wrapper class that does the job:

    int iprim = i.intValue ();
    double dprim = d.doubleValue ();

There are also methods for converting wrapper objects into different primitive types. You should check out the documentation for each wrapper class to see what is available.

Last Update: 2011-01-24