Java wrapper classes

Ask a question
          

Java wrapper classes

NEXT>>

Why do we need wrapper classes?
What is wrapper classes in Java.
Why do we need wrapper classes in Java?
Features of the Java wrapper Classes.
How to use wrapper classes in java? Explain with an example.
List out the primitive types and the corresponding wrapper classes in java
There are some of the methods of the Wrapper class which are used to manipulate the data. Explain them

Java Wrapper Class - Aug 11, 2009, 22:00 pm by Amit Satpute

Why do we need wrapper classes?

Answer
Wrapper classes are used to be able to use the primitive datatypes as objects.


Java Wrapper Class - Feb 28, 2009, 19:00 pm by Vidya Sagar

What is wrapper classes in Java.

Wrapper classes allow primitive data types to be accessed as objects. They are one per primitive type: Boolean, Byte, Character, Double, Float, Integer, Long and Short. Wrapper classes make the primitive type data to act as objects.

Why do we need wrapper classes in Java?

Dealing with primitives as objects is easier at times. Most of the objects collection store objects and not primitive types. Many utility methods are provided by wrapper classes. To get these advantages we need to use wrapper classes. As they are objects, they can be stored in any of the collection and pass this collection as parameters to the methods.

Features of the Java wrapper Classes.

  • Wrapper classes convert numeric strings into numeric values.
  • The way to store primitive data in an object.
  • The valueOf() method is available in all wrapper classes except Character
  • All wrapper classes have typeValue() method. This method returns the value of the object as its primitive type.

How to use wrapper classes in java? Explain with an example.

Java uses primitive types and are part of any object hierarchy. These values are passed to methods by values.

Character class methods:

        isDigit() – to determine whether the character is digit.
        isLower() – to determine whether the character is lower case alphabet.
        is Letter() – to determine whether the character is an alphabet.

Ex:

if(Character.isDigit(a[i]))
           System.out.println(a[i] + "is a digit ");
if(Character.isLetter(a[i]))
           System.out.println(a[i] + "is a letter ");

Byte class methods:

byteValue() – returns the Byte value as byte value
           Ex : byte bvalue = Byte.byteValue();
parseByte() – returns byte value from a byte string
           Ex: byte bvaue = Byte.parseByte(“93”);

Integer class methods:

intValue() – returns the Integer value as int value
        Ex: int bvalue = Integer.intValue();
parseInt() – returns int value from a int string
        Ex: int bvaue = Integer.parseInt(“73”);

List out the primitive types and the corresponding wrapper classes in java

Primitive Data Types Wrapper class
byte Byte
short Short
int Integer
long Long
char Character
float Float
double Double
boolean Boolean

There are some of the methods of the Wrapper class which are used to manipulate the data. Explain them

Some of the data manipulation methods from Byte and Integer classes are:

Byte Class

static Byte decode(String nm): A string is decoded into Byte
double doubleValue(): Returns the value of Byte as double value
byte parseByte(String str): Returns byte value of the given byte string
static String toString(byte b): Returns String object representing the byte type
byte byteValue(): Returns byte value of the Byte object

Integer Class

byte byteValue(): Returns byte value of the Integer object
int intValue(): Returns int value of the Integer object
static int reverse(int ivalue): Returns the value after reversing the order of bits specified in two’s complement for an int value.
static String toBinaryString(int inum): Returns the String representation of int as an unsigned integer in base 2.
static String toHexString(int inum): Returns the String representation of int as an unsigned integer in base 16.
String toString(): Returns a string object that represents this Integer’s value.

NEXT>>


Write your comment - Share Knowledge and Experience


Related Java links

What is an abstract class? | Explain the difference between abstract class and interfaces.

An abstract class defines an abstract concept which can’t be.....

What is an interface?

An interface is a set of method definition without implementation. It is a protocol....

Explain how to implement polymorphism in JAVA.

Capacity of a method to do different things based on the object that......

What is the difference between AWT and Swing?

Classes in swing are not OS dependent. They don’t create peer components,..........

What is meant by Stream Tokenizer?

The StreamTokenizer class takes an input stream and parses it into "tokens", allowing.........

What is the advantage of the event-delegation model over the earlier event inheritance model?

Event-delegation model allows a separation between a component's design and its use as it enables event handling to be handled by objects other than the ones that generate the events.......................

How are Observer and Observable used?

The observable class represents an observable object.
The object to be observed can be represented by sub-classing observable class..................

 
Interview questions
 
Home | Login | About us | Sitemap | Contact us