please tell me what is wrapper class in java with example ?

Answer Posted / vikneswarank

its a class used to convert primitive data type to user
defined data type like Integer

for example

public class WrapperSample1
{
public static void main (String args[])
{
int iVal = 100;
long lVal = 200;
float fVal = 300;
double dVal = 400;

String str = "123";

//primitive types to corresponding wrapper objects
Integer intVal = Integer.valueOf(iVal);
Long lgVal = Long.valueOf(lVal);
Double dbVal = Double.valueOf(dVal);
Float ftVal = Float.valueOf(fVal);

//primitive types to string objects
str = String.valueOf(iVal);
str = String.valueOf(lVal);
str = String.valueOf(fVal);
str = String.valueOf(dVal);

//wrapper Objects to corresponding primitive types
iVal = intVal.intValue();
lVal = lgVal.longValue();
fVal = ftVal.floatValue();
dVal = dbVal.doubleValue();

//String to primitive types
iVal = Integer.parseInt(str);
lVal = Long.parseLong(str);
fVal = Float.parseFloat(str);
dVal = Double.parseDouble(str);

//string to wrapper objects
intVal = Integer.valueOf(str);
lgVal = Long.valueOf(str);
ftVal = Float.valueOf(str);
dbVal = Double.valueOf(str);
}
}

Is This Answer Correct ?    60 Yes 17 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a boolean structure?

783


Tell me are there implementations for sorting and searching in the java libarary?

816


What is the difference between a static and a non-static inner class in java programming?

727


FOR EXAMPLE WE R HAVING TWO LIST ELEMENTS ..BOTH LISTS CONTAINS ID,NAME,PLACE ..I NEED TO COMPARE BOTH IDS IN TWO LISTS,IF ID'S R SAME MEANS WE HAVE ADD THE DETAILS(LIKE NAME,PLACE) TO MAP...HOW IS POSSIBLE ?CAN ANY ONE SUGGEST?

2972


Difference between vector and arraylist.

781


What is static keyword in java?

757


what is heap memory?

854


What are the major drawbacks of external iteration?

805


Explain different types of thread priorities ?

850


Differentiate between overriding and overloading cases?

796


What is the use of list in java?

739


Explain the importance of finalize() method.

750


how to know the total memory occupied by the objects in the ArrayList(Array list may contain duplicate objects)

2114


What is instance synchronization?

811


Is final static java?

770