Witscale Test Center

13.5 The wrapper classes > 13.5.5 How to get string objects from the wrapper objects?


13.5.5 How to get string objects from the wrapper objects?

There are in all three kinds of methods in wrapper classes that return a String object representing the primitive value.

1.       All wrapper classes have a instance method toString() that returns a string representing the primitive value wrapped by the wrapper.

2.       Only the six numeric wrapper class has a static method toString() that takes the corresponding primitive value as argument and returns a string representation of it.

3.       Only the Integer and Long wrapper classes has three static methods (toHexString(),toOctalString() and toBinaryString(). These methods return a string object in three different number systems respectively.

The instance method toString()

The wrapper classes inherit the toString() method from the Object class. They override it to provide some meaningful representation of themselves. All wrapper classes provide this method to return  a String representing the value of the primitive they wrap.  The signature of this method is as:

 

public String toString()

 

For example, if you invoke toString() method of a Float object that wraps 100.5f, the string representing 100.5f is returned. You can invoke this method as,

 

Float f = new Float("100.5");

System.out.println("The float value is " + f.toString() );

 

The float value is 100.5” is printed.

The static method toString(<primitiveType>)

All numeric wrapper classes have a static toString() method that takes a primitive value of the appropriate type and returns a string representation for that primitive value. The signature of this method in Integer class is as:

 

public static String toString(int i)

 

Every numeric class will have this method taking the value of corresponding primitive type as argument. For instance, you can invoke the Double.toString() that takes a double argument as:

 

System.out.println("The double value is " + Double.toString(100.5));

 

Thus, all six numeric wrapper classes have this method. The classes Integer and Long provide one more static toString() method . This overloaded method takes a primitive value as first argument. Its second argument is a radix indicating the number system in which the first argument is to be converted. When you invoke this method, it converts the first argument to the number system mentioned in radix and returns the result as a String. You can invoke this method to get a hexadecimal equivalent of a number as:

 

String hexNo = Integer.toString(100,16); // radix is 16

System.out.println("Hexadecimal equivalent of 100 is " + hexNo);

After invoking this method, “Hexadecimal equivalent of 100 is 64” is printed.

 

to<numberSystem>String() (Binary, Hexadecimal, Octal)

The Integer and Long wrapper classes provide three static methods that return a string representation of the argument in binary, octal or hexadecimal number systems. Following are the method signatures in class Integer.

 

public static String toHexString(int i)

public static String toOctalString(int i)

public static String toBinaryString(int i)

 

The Long class has similar three methods but they take long as the argument (instead of int). These methods return a string representation of the argument as an unsigned integer. It means that string representation would be exactly same, as the argument value would look like in the given number system. However, it will not include sign or leading zeros (if any). You can invoke these methods to find out how a particular value say 100L (in decimal system) is presented in other number systems. For example,

 

long longVal = 100L;

System.out.println("100L in hexadecimal: " + Long.toHexString(longVal));

System.out.println("100L in octal system: " + Long.toOctalString(longVal));

System.out.println("100L in binary system: " + Long.toBinaryString(longVal));

 

The output will be...

 

100L in hexadecimal: 64

100L in octal system: 144

100L in binary system: 1100100

 

These three methods are useful to find the equivalent representation of a number in any of the three number systems especially when you do not know how the actual conversion takes place.