Wrapper Classes

A wrapper class contains a field where it stores the primitive datatype. When we create an object to a wrapper class, it carries the primitive datatype within it and hence the object can be sent to the server. The server can retrieve the primitive datatype from the object and use it.

Why Do We Need Wrapper Classes?

  • Wrapper class convert primitive data types into objects and this is needed on internet to communicate between two applications.

  • The classes in java.util package handle only objects and hence wrapper classes help in this case also.

  • Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.

  • An object is needed to support synchronization in multithreading

Below is the list of wrapper classes that are defined in java.lang package.

Primitive Data TypeWrapper Class
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
booleanBoolean

Number Wrapper Class

Number class is an abstract class whose subclasses are Byte, Short, Integer, Long, Float, and Double. The number class includes the following methods

  • byte byteValue() - This method converts the calling object into byte value

  • short shortValue() - This method converts the calling object into short value

  • int intValue() - This method converts the calling object into int value

  • long longValue() - This method converts the calling object into long value

  • float floatValue() - This method converts the calling object into float value

  • double doubleValue() - This method converts the calling object into double value

Character Wrapper Class

The character class wraps a value of the primitive type char in an object, if we create character class object, it contains a char type field. Important methods of Character class are as follows

  • char charValue() - This method is useful to convert character class object again into primitive char value and returns that value

  • int compareTo(Character obj) - This method is useful to compare two character objects

  • String toString() - This method converts character object into string object and returns that string object

  • static Character valueOf(char ch) - This method converts single character ch into character object and returns that object

  • static boolean isDigit(char ch) - This method returns true if ch is a digit or else returns false

  • static boolean isLetter(char ch) - This method returns true if ch is a letter or else returns false

  • static boolean isUpperCase(char ch) - This method returns true if ch is an upper case letter

  • static boolean isLowerCase(char ch) - This method returns true if ch is a lower case letter

  • static boolean isSpaceChar(char ch) - This method returns true if ch represent a space which is coming from spacebar

  • static boolean isWhiteSpace(char ch) - This method returns true if ch represent whitespaces which is coming from pressing tab, return, and backspace button

  • static boolean isLetterOrDigit(char ch) - This method returns true if ch is either letter or a digit

  • static char toUpperCase(char ch) - This method converts ch into uppercase and returns that upper case letter

  • static char toLowerCase(char ch) - This method converts ch into lower case and returns that lower case letter

Following is the program to understand the methods of character wrapper class


public class Test {
    public static void main(String[] args) {
        System.out.println(Character.toString('x'));
        System.out.println(Character.toString('Y'));
        System.out.println(Character.isLowerCase('A'));
        System.out.println(Character.isLowerCase('a'));
        System.out.println(Character.isLowerCase(97));
        System.out.println(Character.isUpperCase('A'));
        System.out.println(Character.isUpperCase('a'));
        System.out.println(Character.isUpperCase(65));
        System.out.println(Character.isDigit('A'));
        System.out.println(Character.isDigit('0'));
        System.out.println(Character.isLetter('A'));
        System.out.println(Character.isLetter('0'));
        System.out.println(Character.isWhitespace('A'));
        System.out.println(Character.isWhitespace(' '));
        System.out.println(Character.isWhitespace('\n'));
        System.out.println(Character.isWhitespace('\t'));
        System.out.println(Character.toUpperCase('a'));
        System.out.println(Character.toLowerCase('A'));
    }

}

The output is

x
Y
false
true
true
true
false
true
false
true
true
false
false
true
true
true
A
a

Byte Wrapper Class

The byte class wraps a value of the primitive type byte in an object. This Byte class object contains a byte type field. Important methods of Byte class are as follows

  • static byte parseByte(String str) - This method returns the primitive byte number contained in the string str.

  • String toString() - This method converts byte object into string object and returns that string object.

  • static Byte valueOf(String str) - This method converts a string str that contains some byte number into byte class object and returns that object.

  • static Byte valueOf(Byte b) - This method converts the primitive byte b into byte object.

  • int compareTo(Byte b) - This method is useful to compare the contents of two bytes class object.

// creating Byte class objects and comparing them
class ByteDemo {
    public static void main(String[] args) throws IOException {
        // to accept data from keyboard
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        // accept a byte number as string s1
        System.out.println("Enter a byte no: ");
        String s1 = br.readLine();
        //create Byte object b1 using s1
        Byte b1 = new Byte(s1);
        // accept a byte number as string s1
        System.out.println("Enter a byte no: ");
        String s2 = br.readLine();
        //create Byte object b2 using s2
        Byte b2 = new Byte(s2);
        // compare both the Byte objects content
        int n = b1.compareTo(b2);
        if (n == 0)
            System.out.println("Both bytes are same");
        else if (n < 0) System.out.println(b1 + "is less");
        else System.out.println(b2 + "is less");

    }
}

The output is

Enter a byte no: 
120
Enter a byte no: 
123
120is less

Short Wrapper Class

Short class wraps a value of primitive data types short in its object. Short class object contains a short type field that stores a short number. Important methods of Short class are as follows

  • String toString() - This method returns a string form of short object

  • int compareTo(Short obj) - This method compares the numerical value of two short class objects and returns 0, -venumber, or +ve number.

  • static Short valueOf(String str) - This method converts a string str that contains some short number into short class object and returns that object

  • boolean equals(Object obj) - This method compares the short object with any other object obj.

  • static Short parseShort(String str) - This method returns int equivalent of the string.

Integer Wrapper Class

Integer wrapper class is used to create an object version of a primitive int value. Important methods of Integer class are as follows

  • int compareTo(Integer b) - Returns a zero if the invoked Integer object contains the same value as b. Returns a positive value if the invoked Integer object contains greater value than b. Returns a negative value if the invoked Integer object contains smaller value than b.

  • boolean equals(Object ob) - Returns a true if invoked Long object has same value as referred by ob, else false.

  • static int parseInteger(String s) - Returns a primitive int value if String, s could be converted to a valid int value.

  • static Integer valueOf(int b) - Returns an Integer object after converting it from a primitive int value, b.

  • static Integer valueOf(String s) - Returns an Integer object after converting it from a String, s.

import java.io.*;
// convert int into binary and hexadecimal
class Convert {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter an integer: ");
        String str = br.readLine();
        // convert string into int
        int i = Integer.parseInt(str);
        System.out.println("In decimal: " + i);
        // convert int into other systems
        str = Integer.toBinaryString(i);
        System.out.println("In binary: " + str);

        str = Integer.toHexString(i);
        System.out.println("In hexadecimal: " + str);

    }
}

The output is

Enter an integer: 
456
In decimal: 456
In binary: 111001000
In hexadecimal: 1c8

Long Wrapper Class

Long wrapper class is used to create an object version of a primitive long value. Important methods of Long class are as follows

  • int compareTo(Long b) - Returns a zero if the invoking Long object contains value same as b.

Returns a positive value if invoking Long object’s value is > b. Returns a negative value if invoking Long object’s value is < b.

  • boolean equals (Object ob) - Returns a true if invoking Long object has same value as referred by ob, else false.
  • static long parseLong(String s) - Returns a primitive long value if String, s could be converted to a valid long value.
  • static Long valueOf(long b) - Returns a Long object after converting it from primitive long value, b.
  • static Long valueOf(String s) - Returns a Long object after converting it from a String, s.

Float Wrapper Class

Float wrapper class is used to create an object version of a primitive float value. Important methods of Float class are as follows

  • int compareTo(Float b) - Returns a zero if the invoked Float object is same as value of b.

Returns a positive value if the invoked Float object is > b. Returns a negative value if the invoked Float object is < b.

  • boolean equals(Object ob) - Returns a true if invoked Float object has same value as referred by ob, else false.

  • static float parseFloat(String s) - Returns a primitive float value if String, s could be converted to a valid float value.

  • static Float valueOf(Float b) - Returns a Float object after converting it from primitive float value, b.

  • static Float valueOf(String s) - Returns a Float object after converting it from a String, s.

Double Wrapper Class

Double wrapper class is used to create an object version of a primitive double value. Important methods of Double class are as follows

  • boolean doubleValue() - Returns a primitive double value out of Double wrapper object.

  • int compareTo(Double b) - Returns a zero if the invoked Double object contains the same Double value as b.

Returns a positive value if the invoked Double object contains greater value than b. Returns a negative value if invoked object contains smaller value than b.

  • boolean equals(Object ob) - Returns a true if invoking Double object has same value as referred by ob, else false.

  • static Double parseDouble(String s) - Returns a primitive double value converted from a String value.

  • static Double valueOf(double b) - Returns a Double object after converting it from primitive Double value, b.

  • static Double valueOf(String s) - Returns a Double object after converting it from a String, s.

Boolean Wrapper Class

Boolean wrapper class is used to create an object version of a primitive boolean value. Important methods of Boolean class are as follows

  • boolean booleanValue() - Returns a primitive boolean value out of Boolean wrapper object.

  • int compareTo(Boolean b) - Returns a zero if the invoking Boolean object contains the same value as b.

Returns 1 if invoking object contains true and b contains false. Returns -1 if invoking object contains false and b contains true.

  • boolean equals(Object ob) - Returns a true if invoking Boolean object has same value as referred by ob, else false.

  • static boolean parseBoolean(String s) - Returns true if String contains true(irrespective of its case), otherwise false.

  • static boolean valueOf(boolean b) - Returns a Boolean object, equivalent to b.

  • static boolean valueOf(String s) - Returns true if String contains true(irrespective of its case), otherwise false.

Subscribe For More Content