Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Wrapper-class Wrapper Classes

Related Tutorials


 
 

Share on Google+Share on Google+

Wrapper Classes

Advertisement
In this section you will learn about Wrapper classes and all the methods that manipulate data and allows to operate a certain work.

Wrapper Classes

     

 In this section you will learn about Wrapper classes and all the methods that manipulate data and allows to operate a certain work.

Wrapper class is a wrapper around a primitive data type. It represents primitive data types in their corresponding class instances e.g. a boolean data type can be represented as a Boolean class instance.
All of the primitive wrapper classes in Java are immutable i.e. once assigned a value to a wrapper class instance cannot be changed further.

Wrapper Classes are used broadly with Collection classes in the java.util package and with the classes in the java.lang.reflect reflection package.

Following table lists the primitive types and the corresponding wrapper classes:

Primitive Wrapper
boolean   java.lang.Boolean
byte   java.lang.Byte
char   java.lang.Character
double   java.lang.Double
float   java.lang.Float
int   java.lang.Integer
long   java.lang.Long
short   java.lang.Short
void   java.lang.Void
 

In Java 5.0 version, additional wrapper classes were introduced in the java.util.concurrent.atomic package. They provide atomic operations for assignment, addition and increment. These classes act like variables  and cannot be used as a substitute for the regular wrapper classes. Few of these new wrapper classes like AtomicInteger and AtomicLong are the subclasses of the Number Classes.

Primitive Wrapper
boolean   AtomicBoolean
int   AtomicInteger
long   AtomicLong
V   AtomicReference<V>

Features Of the Wrapper Classes

Some of the sound features maintained by the Wrapper Classes are as under :

  • All the methods of the wrapper classes are static.
  • The Wrapper class does not contain constructors.
  • Once a value is assigned to a wrapper class instance it can not be changed, anymore.

Wrapper Classes : Methods with examples

There are some of the methods of the Wrapper class which are used to manipulate the data. Few of them are given below:

1.  add(int, Object): Learn to insert an element at the specified position.

2.  add(Object): Learn to insert an object at the end of a list.

3.  addAll(ArrayList): Learn to insert an array list of objects to another list.

4.  get(): Learn to retrieve the elements contained with in an ArrayList object.

5.  Integer.toBinaryString(): Learn to convert the Integer type object to a String object.

6.  size(): Learn to get the dynamic capacity of a list.

7.  remove(): Learn to remove an element from a particular position specified by a  index value.

8.  set(int, Object): Learn to replace an element at the position specified by a  index value.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 28, 2008

Related Tutorials

Discuss: Wrapper Classes   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
pratik
August 23, 2011
regards wrapper class

i understood features of it.but can't unterstand how to define and use wrapper class.
preeti kumari
March 3, 2013
java questions on exceptions

class Abc{ public static void main (String args[]){ try{ System.out.println("I m trying"); } } }
rose
October 3, 2011
wrapper

awesome...nice explanatino...
Anusha
December 13, 2011
wrappers classes

As mentioned in this site , the statement 'wrapper classes doesnt contain constructors' is wrong .wrapper classes contains constructors,character contains only one , double contains three , where as rest of them contains two constructors.for more clarity check the API
Shanti
February 11, 2012
Wrapper classes

How we fix the precision in wrapper classes in java
Niraj
January 8, 2013
Wrong Information

You mentioned in this tutorial : 1. All the methods of the wrapper classes are static. - whereas there are many non-static methods appear in java.lang.Integer class, example getShort(), getXxx(). 2. The Wrapper class does not contain constructors. - but two constructors in Integer(int value) and Integer(String s) are there.
DMCA.com