answersLogoWhite

0


Best Answer

java uses simple or primitive data types, such as int, char and Boolean etc. These data types are not part of the object hierarchy. They are passed by value to methods and cannot be directly passed by reference. However, at times there is a need to create an object representation of these simple data types. To address this need, Java provides classes that correspond to each of these simple types. These classes encapsulate, or wrap, the simple data type within a class. Thus, they are commonly referred to as wrapper classes.

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

eg

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();

User Avatar

Wiki User

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

Wiki User

13y ago

The wrapper classes in the Java API serve two primary purposes:

• To provide a mechanism to "wrap" primitive values in an object so that the primitives can be included in activities reserved for objects, like being added to Collections, or returned from a method with an object return value.

• To provide an assortment of utility functions for primitives. Most of these functions are related to various conversions: converting primitives to and from String objects, and converting primitives and String objects to and from different bases (or radix), such as binary, octal, and hexadecimal.

An Overview of the Wrapper Classes

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");

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Java program to illustrate Wrapper class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is wrapper class in java?

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


Why you start java program by class?

without class non of the folder can run so the java program should start in class we can use the class without object in java


What will happen if a Java Class has no Main Method?

Nothing will happen. There is no restriction that every Java class must have a main method. The only program is that, this class cannot be executed as a standalone java program.


Which one is the compulsory section in a java program?

Class declaration


How do you execute a Java program?

You can run a Java application from the command line using "java <name of the class>"


Is class is mandatory in program?

In C, no. In Java, yes.


In java a class will take memory?

Yes, executing a Java class will require memory just like any other program.


What do you get when you compile a java program?

The java interpreter or JVM (Java Virtual Machine) is not able to execute the java source code for a program. The java source code first needs to be compiled into bytecode that can be processed by JVM. Producing bytecode make the program platform independent as each platform has its own JVM. It is also possible to directly write bytecode, bypassing the need to compile, but that would be tedious job and also not good for security purpose as the compiler checks for various errors in a program.


Why you same name in class and file in java?

For run the program


What is the command to compile a java program named Greetingsjava?

The command to compile a Java program is "javac", followed by the class name (file name).


How do you run a java program form the command prompt?

The command is "java", followed by the class name. For example, if your compiled class is called myclass.class, you give the command: java myclass


Where java program started?

If it is already compiled, you can start your Java program from the command line. Just type java myclass replacing "myclass" with the program you want to start - a program with the ".class" extension, for example, myclass.class.