Sei sulla pagina 1di 9

WRAPPER CLASSES

type

Wrapper class

boolean

Boolean

char

Character

double

Double

float

Float

int

Integer

long

Long

Converting primitive numbers to object


number using constructor methods
Constructor

Conversion action

Integer v1=new Integer(i)

Primitive integer to integer object

Float v1=new Float(f)

Primitive float to Float object

Double v1=new Double(d)

Primitive double to Double object

Long v1=new Long(l)

Primitive long to long object

Converting object numbers to primitive


numers
Method

Conversion action

Int i=v1.intValue();

Object to primitive integer

float f=v1.floatValue();

Object to float

long l=v1.longValue();

Object to primitive long

double d=v1.doubleValue();

Object to primitive double

Converting object numbers to string


Method

Conversion action

Str=Integer.toString(i)

Primitive integer to string

Str=Float.toString(f);

Primitive float to string

Str=Double.toString(d);

Primitive double to string

Str=Long.toString(l)

Primitive long to string

Converting string object to numeric


object
Method

Conversion action

D=dobule.ValueOf(str)

Converts string to Double object

F=Float.ValueOf(str)

Converts string to Float object

I=Integer.ValueOf(str)

Converts string to Integer object

L=Long.ValueOf(str)

Converts string to Long object

Converting numeric string to primitive


numbers using parsing methods
Method

Conversion action

int i=Integer.parseInt(str);

Converts string to primitive integer

long i=Long.parseLong(str);

Converts string to primitive long

class wrapperclassex
{
public static void main(String args[])
{
Float p=new Float(0);
Integer a=0;
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("enter Float value");
String F=in.readLine();
p=Float.valueOf(F);
System.out.print("Enter integer value");

System.out.flush();
String I=in.readLine();
a=Integer.parseInt(I);
}
catch(IOException e)
{
System.out.println("I/O Error");
System.exit(1);
}
System.out.println(p.floatValue()));
System.out.println(a);
}}

Potrebbero piacerti anche