Monday, 24 March 2014

WRAPPER CLASSES IN JAVA WITH EXAMPLE

Posted By: Unknown - Monday, March 24, 2014


Welcome in another post “Wrapper Classes in Java with example” in 
Primitive data type are the integral parts of language but they are not object. You can visit my post ‘Data type in Java’ for more information on primitive data type. You can not garbage collect ‘primitive data type’ directly. Wrapper classes allows primitive types to be converted as object and object can also be converted to primitive data types. One real example of real need of wrapper class is that We can not add primitve data type in collection and we need to do it very frequently.
Wrapper wraps primtive data type in the object of wrapper so they can be used as object. We have similar number of wrapper classes as primitive data type. We can add wrapper and can also remove wrapper to get again primitive data type. Java have eight primitive data types so we have eight wrapper classes for the same.
Please provide your valuable feedback on this post from Guru Java (GuruJava.in). It will encourage me to write some more post.
Primitive Data Type
Wrapper Class
Convert Primitive Data
Type to Object
(Adding Wrapper)
Get Primitive Data type from                 Wrapper class object
byte
Byte
Byte byteObj=new Byte((byte)12);
byte b = byteObj.byteValue();
short
Short
Short shortObj=new Short((short)12);
short b = shortObj.shortValue();
int
Integer
Integer intObj=new Integer(12);
int b = intObj.intValue();
long
Long
Long longObj=new Long(12l);
long b = longObj.longValue();
float
Float
Float floatObj=new Float(12f);
float b = floatObj.floatValue();
double
Double
Double dblObj=new Double(12);
double b = dblObj.doubleValue();
char
Character
Character chrObj=new Character((char)65);
char c = charObj.charValue();
boolean
Boolean
Boolean boolObj=new Boolean(true);
boolean bool = boolObj.booleanValue();
Wrapper Class Example in Java :
class WrapperClasses
{
 public static void main(String args[])
 {

  Byte byteObj=new Byte((byte)12);
  byte b = byteObj.byteValue();
  System.out.println("byte :"+b);

  Short shortObj=new Short((short)12);
  short s = shortObj.shortValue();
  System.out.println("short :"+s);

  Integer intObj=new Integer(12);
  int i = intObj.intValue();
  System.out.println("int :"+i);

  Long longObj=new Long(12l);
  long l = longObj.longValue();
  System.out.println("long :"+l);

  Float floatObj=new Float(12f);
  float f = floatObj.floatValue();
  System.out.println("float :"+f);

  Double dblObj=new Double(12);
  double d = dblObj.doubleValue();
  System.out.println("double :"+d);

  Character chrObj=new Character((char)65);
  char c = chrObj.charValue();
  System.out.println("char :"+c);

  Boolean boolObj=new Boolean(true);
  boolean bool = boolObj.booleanValue();
  System.out.println("bool :"+bool);
 }
}
Output of Wrapper Class Example :
G:\personal>java WrapperClasses
byte :12
short :12
int :12
long :12
float :12.0
double :12.0
char :A
bool :true

0 comments:

Post a Comment

this blog is helpful or not

International

Auto News

Translate

Pages

Popular Posts

Popular Posts

Designed By Templatezy / Sb Game Hacker Apk / MyBloggerThemes