Page 330 - CA_Blue( J )_Class10
P. 330
13.5 HOW TO USE WRAPPER CLASSES?
To use the wrapper classes in our program, we need to import the java.lang package using the import statement in the
following manner:
import java.lang.*;
Using the preceding code, all the wrapper classes are imported in the program. We can also import a particular wrapper
class in the following manner:
import java.lang.Integer;
Preceding code imports only the Integer class. After importing the java.lang package, we can use the methods of the
wrapper classes in the following way:
Wrapper_Class_Name.method_Name()
13.5.1 Methods to convert Primitive Data Types to String
Every wrapper class contains a method toString(). Some of them are:
• Interger.toString(int): The toString() method of the Integer class is used to convert an integer value into string. It
takes an integer value as parameter and returns after converting it into string. Syntax of the toString() method is:
String variable = Integer.toString(int);
Let us take an example:
int a = 12;
String s = Integer.toString(a);
System.out.println("Result: " + s);
Output:
Result: 12
Note: After converting an integer value into a string value, no calculation can be done on the value.
• Long.toString(long): The toString() method of the Long class is used to convert a long value into string. It takes a
long value as parameter and returns after converting it into string. Syntax is:
String variable = Long.toString(long) ;
Let us take an example:
long ln = 341235467;
String sl = Long.toString(ln);
System.out.println("Result: " + sl);
Output:
Result: 341235467
• Double.toString(long): The toString() method of the Double class is used to convert a double value into string. It
takes a double value as parameter and returns after converting it into string. Syntax is:
String variable = Double.toString(double) ;
Let us take an example:
double dn = 34.6;
String sl = Double.toString(dn);
System.out.println("Result: " + sl);
Output:
Result: 34.6
• Float.toString(long): The toString() method of the Float class is used to convert a float value into string. It takes a
float value as parameter and returns after converting it into string. Syntax is:
String variable = Float.toString(float);
328328 Touchpad Computer Applications-X

