Java wrapper classes

What are 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.

1. Wrapper classes convert numeric strings into numeric values.
2. The way to store primitive data in an object.
3. The valueOf() method is available in all wrapper classes except Character
4. 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:

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

Example:
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
Example:
byte bvalue = Byte.byteValue();
parseByte() – returns byte value from a byte string
Example:
byte bvaue = Byte.parseByte(“93”);

Integer class methods:

intValue() – returns the Integer value as int value
Example:
int bvalue = Integer.intValue();

parseInt() – returns int value from a int string
Example:
int bvaue = Integer.parseInt(“73”);

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

Primitive Data TypesWrapper class
byteByte
shortShort
intInteger
longLong
charCharacter
floatFloat
doubleDouble
booleanBoolean

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 2’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.
Java error exception: What is the difference between error and an exception?
Java error exception - Errors are abnormal conditions that should never occur. Not to be confused with the compile time errors....
What is the difference between preemptive scheduling and time slicing?
Java pre-emptive scheduling and time slicing - Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks...
Java serializable Interface - What is serializable Interface?
Java serializable Interface - If we want to transfer data over a network then it needs to be serialized. Objects cannot be transferred as they are...
Post your comment
Discussion Board
best one
best explanation
thanks
arun singh 02-21-2017