answersLogoWhite

0

What is wrapper class and its use?

Updated: 8/10/2023
User Avatar

Royakashmca

Lvl 1
13y ago

Best Answer

When we need to store primitive datatypes(The data types we use in genera like:int,long,float etc)as objects, we use

wrapper classes.Means in utility classes all the utility

classes stores Objects.So when we need to store a primitive

datatype,We make an object of that primitive data and store it.

Say supposing there is a requirement to store only the

object in an array A.The Primitive types cannot be stored in

the same array as the array can accommodate only Objects

here is where Wrapper Class come into picture.ie, we create

wrapper for the primitive types.One such example is as below

Ex:int i;

Wrapper class for the primitive type(int) is created as below:

Integer i = new Integer();

That's why we use Wrapper classes in Java

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

There is a wrapper class for every primitive in Java. For instance, the wrapper class for int is Integer, the class for float is Float, and so on. Remember that the primitive name is simply the lowercase name of the wrapper except for char, which maps to Character, and int, which maps to Integer.

The Wrapper classes for each of the primitive types is as follows:

1. boolean - Boolean

2. byte - Byte

3. char - Character

4. double - Double

5. float - Float

6. int - Integer

7. long - Long

8. short - Short

A point to note here is that, the wrapper classes for numeric primitives have the capability to convert any valid number in string format into its corresponding primitive object. For ex: "10" can be converted into an Integer by using the below line

Integer intVal = new Integer("10");

I can do the same for all numeric types like long, short etc. But, if I pass an invalid value I will get a "NumberFormatException" Ex:

Integer intVal = new Integer("Haha");

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

wrapper class: Wrapper classes are classes that are used to make primitive

datatypes into objects, and to make wrapped objects into

primitives.Primitive data types like int, boolean, double

and their respective wrapper classes are Integer, Boolean,

and Double.

Wrapper classes corresponding to respective simple data types are as given in table below.

Primitive Data Types

Wrapper class

byte

Byte

short

Short

int

Integer

long

Long

char

Character

float

Float

double

Double

boolean

Boolean

void

Void

use: When we need to store primitive datatypes as objects,we

use wrapper classes means in utility classes.All the

utility classes stores Objects.So when we need to store a

primitive datatype,we make an object of that primitive

data and store it.

Say supposing there is a requirement to store only the object in an array.The Primitive types cannot be stored in the same array,as the array can accommodate only Objects.Here is where Wrapper Class come into picture.ie, we create wrapper for the primitive types.One such example is as below.

It is also used to covert primitive datatypes into string/character.

i.e. Integer.parseInt()

Ex:int i;

Wrapper class for the primitive type(int) is created as below:

Integer i = new Integer();

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Wrapper classes to exactly what they say, they wrap primitive types (eg: int, double, etc) in Objects which can be placed into Vectors, and many, many other uses. *Notice that an Object starts with a capital letter, while the primitives all start with a lowercase. Also notice that Strings are Ojects. These Wrapper classes can be created in many ways, so i will start slow, simply returning objects with a String. Ex: Integer from String: Integer i = Integer.valueOf("125");

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is wrapper class and its use?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Which is the wrapper class for the data type float?

The wrapper class for float is Float. java.lang.


What is wrapper class in java?

wrapper class is a predefined class .it is used for converting primitive data types into object type


Is wrapper class final?

yes


Which wrapper class cannot take string in constructor?

useless Q


What kind of gum still comes with a wrapper you can use to make a gum wrapper chain?

Stride?


How do you use wrapper in a sentence?

She undid the candy wrapper. Annalise played with the wrapper as she ate the Snickers bar. Ginny spit her gum back into its wrapper and threw it out. The wrappers on the candy bars had been shredded.


What are wrapped classes?

Wrapped classes are wrapped by Visible ones, which means wrapped ones should be invisible. Adapter Pattern is very closely related to wrapper, because it comes with 2 different flavors: object wrapper and class wrapper. There is also another way to define "wrapped" classes: a private class defined within another class. For example: class TheWrapper { private BeingWrapped _internal; } class TheVisibleOne { private class WrappedClass { } // this class is only accessible in TheVisibleOne } BeingWrapped and WrappedClass are the ones being wrapped/hide by other classes.


How do you use the word write in a sentence?

how do you use the word wrapper in a sentence I would like to write a short note. I will pick up the candy wrapper.


What is meant by wrapper classes?

Java has several "primitive" types (byte, short, int, float, double, boolean, char). Wrapper classes refer to the classes which "wrap up" each of these. For example, the Byte class contains a byte primitive and the methods to modify the class.


How do you use sanitary in a sentence?

a sanitary wrapper on all sandwiches.


How do you build a bank out of popsicle sticks?

You can get a candy wrapper then glue (Iwould use a hot glue gun) the sticks to the wrapper and test it out:)


Will a hershey bar melt quicker with its wrapper or with out its wrapper in the sun?

with its wrapper because the heat will stay trapped inside the wrapper