Monday, June 22, 2009

How to Wrape a Primitive Type in a Wrapper Object


    // Create wrapper object for each primitive type
Boolean sampleBoolean = new Boolean(
true);
Byte sampleByte = new Byte((byte)245
);
Character sampleChar = new Character(
'Y');
Short sampleShort = new Short((short)
245);
Integer sampleInt = new Integer(
245);
Long sampleLong = new Long(
245L);
Float sampleFloat = new Float(
245F);
Double sampleDouble = new Double(
24.5D);

// Retrieving the value in a wrapper object
boolean bool = sampleBoolean.booleanValue();
byte b = sampleByte.byteValue();
char c = sampleChar.charValue();
short s = sampleShort.shortValue();
int i = sampleInt.intValue();
long l = sampleLong.longValue();
float f = sampleFloat.floatValue();
double d = sampleDouble.doubleValue();

1 comments:

Anish Antony on March 26, 2015 at 1:06 PM said...

http://www.behindjava.com/2014/06/primitive-and-wrapper-classes-in-java.html

 

Sample Java Codes Copyright © 2008 Green Scrapbook Diary Designed by SimplyWP | Made free by Scrapbooking Software | Bloggerized by Ipiet Notez