Darla SandyKnowledge Contributor
What are the wrapper classes in Java? Provide examples.
What are the wrapper classes in Java? Provide examples.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
Wrapper classes in Java are classes that encapsulate primitive data types and provide utility methods for working with them as objects. They allow primitive data types to be used in contexts that require objects, such as collections, generics, and methods that operate on objects.
1. Integer: Encapsulates the int data type.
“`java
Integer intWrapper = Integer.valueOf(10); // Creating an Integer object
int intValue = intWrapper.intValue(); // Extracting the int value
“`
2. Long: Encapsulates the long data type.
“`java
Long longWrapper = Long.valueOf(100L); // Creating a Long object
long longValue = longWrapper.longValue(); // Extracting the long value
“`
3. Float: Encapsulates the float data type.
“`java
Float floatWrapper = Float.valueOf(3.14f); // Creating a Float object
float floatValue = floatWrapper.floatValue(); // Extracting the float value
“`
4. Double: Encapsulates the double data type.
“`java
Double doubleWrapper = Double.valueOf(3.14159); // Creating a Double object
double doubleValue = doubleWrapper.doubleValue(); // Extracting the double value
“`
5. Byte: Encapsulates the byte data type.
“`java
Byte byteWrapper = Byte.valueOf((byte) 127); // Creating a Byte object
byte byteValue = byteWrapper.byteValue(); // Extracting the byte value
“`
6. Short: Encapsulates the short data type.
“`java
Short shortWrapper = Short.valueOf((short) 1000); // Creating a Short object
short shortValue = shortWrapper.shortValue(); // Extracting the short value
“`
7. Character: Encapsulates the char data type.
“`java
Character charWrapper = Character.valueOf(‘A’); // Creating a Character object
char charValue = charWrapper.charValue(); // Extracting the char value
“`
8. Boolean: Encapsulates the boolean data type.
“`java
Boolean booleanWrapper = Boolean.valueOf(true); // Creating a Boolean object
boolean booleanValue = booleanWrapper.booleanValue(); // Extracting the boolean value
“`