What are Wrapper Classes and why we use in java
Posted By : Vakeel Malik | 31-Oct-2018
When the user does register in any web application then enter some information like email id and mobile number etc, send all these data to the server in form of objects. email id is sent in form of the string and mobile in form of the int. string is a class easily sent to the server but can not send an int to the server in form of an object. because an int is a primitive data type. we need to convert primitive data type to object. the wrapper classes are used to convert primitive data type to object. wrapper classes are also used in collection framework. when a programmer works on java.util.*; the package needs wrapper classes. because java.util.* the package works on the only object, not on the primitive data type.
When we convert primitive data types to objects and wise verse needs some class. this class is called the Wrapper class. the wrapper class also use in the collection. each data type has a corresponding wrapper class
there are some predefined wrapper classes.
Data type Wrapper classes
1. byte Byte
2. short Short
3. int Integer
4. long Long
5. float Float
6. double Double
all wrapper classes are the subclass of Number class. Number class is a superclass of all wrappers class which contains some abstract methods and some non-abstract methods.
there are some methods of number class
1. byteValue()
2. shortValue()
3. intValue()
4. longValue()
5. floatValue()
6. doubleValue()
these all methods are static methods which will call with the class name these methods are used to convert Object to respective primitive data type. all methods will be available in wrapper classes because wrapper class derived from number class.
how to use these methods:-
Example
Program to accept an integer number from keyword and convert it into
import java.io.*;
class DemoWrapper {
public static void main(String arg[]) throws IOException{
System.out.println("enter some inter value");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//receive data from keyword in string form
String data=br.readLine();
// convert to integer
int i=Integer.parseInt(data);
System.out.println("integer "+i);
//convert to decimal
data=Integer.toBinaryString(i);
System.out.println("binary string "+data);
//convert to hexDecimal
data = Integer.toHexString(i);
System.out.println("hex string"+data);
//convert to OctaDecimal
data =Integer.toOctalString(i);
System.out.println("octal string "+data);
}
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Vakeel Malik
Vakeel is a bright Java Developer working in Oodles.