articles

Home / DeveloperSection / Articles / Wrapper Classes: Constructors and Methods

Wrapper Classes: Constructors and Methods

zack mathews3151 27-May-2016
Constructors

To wrap a primitive type into one of these classes, we use the provided class constructor. Generally, each of these classes provides two constructors:

·         one that takes a primitive type 

·         one that takes a string as a parameter.

 For example, we can wrap an integer data type using any of the following statements:

·         Integer n1 = new Integer(5);

·         Integer n2 = new Integer("10");

In the first statement, an integer argument is used, and in the second statement a string argument is used.

xxxValue() Method

We can retrieve and print the value contained in these two objects by using the following program statements:

·         int i1 = n1.intValue();

·         int i2 = n2.intValue();

The intValue method returns an integer representation of the contained number; the println method converts this to a string and prints it to the console.

equals() Method

Because n1 and n2 are objects, we can compare them for equality. For example, the following program statement prints false on comparing the two objects:

System.out.println(n1 + " = " + n2 + " is " + n1.equals(n2));

However, if we had set the value of n2 equal to the integer value held by n1 (both being equal to, say, integer constant 5), will the comparison return true? Yes, the overloaded equals method compares the values of the objects and returns true if they are equal, even though the two objects are different.

compareTo Method

The Integer class also provides a compareTo method that provides a better comparison between the two objects of its type. The method returns :

·    a value less than 0 if this integer is numerically less than the argument integer

·    and it returns a value greater than 0 otherwise;

·    However, if the two Integer objects hold the same numeric value, it returns 0.

 The following comparison will print –1 to the terminal:

System.out.println (n1.compareTo(n2));

And the following comparison will print 1 to the terminal:

System.out.println (n2.compareTo(n1));

parseInt Method

The Integer class also provides a very useful method called parseInt that parses an input string to extract its int representation. We may also specify the radix while parsing the string.

Radix is a Latin word meaning “root,” which is considered a synonym for “base” in the arithmetical sense. For a decimal system, the radix is 10. For an octal system, it is 8.

This is a static method of the class, so we call it without instantiating the class. When we execute the following two statements, the first statement prints 245 to the terminal and the second statement prints 255 to the terminal:

·         System.out.println("The string holds int value: " + Integer.parseInt("245"));

·         System.out.println("The string holds int value: " + Integer.parseInt("FF", 16));

Note that in the second statement, the second parameter specifies the radix that is used during parsing. Thus, the input string FF is considered as a hex number and its decimal value is printed to the terminal.

 If the given string cannot be parsed in the specified radix, the method will throw a NumberFormatException. For example, Integer.parseInt ("FF", 10) will throw an exception. We may specify a radix of our choice while parsing the string. The following statement parses the specified string using octal conversion and prints the corresponding decimal number 64 to the console:

System.out.println("The string holds int value: " + Integer.parseInt("100", 8));

Program Code

The program given here summarizes our discussion :

         public class TypeWrapperApp {
             public static void main(String args[]) throws Exception {
                   // object construction
                   Integer n1 = new Integer(5);
                   Integer n2 = new Integer("10");
                   // object value
                   System.out.println("n1 holds value: " + n1.intValue());
                   System.out.println("n2 holds value: " + n2.intValue());
                   // object equality
                   System.out.println(n1 + " = " + n2 + " is " + n1.equals(n2));
                   // object comparison
                   System.out.println(n1 + " compared to " + n2 + " returns "
                                      + n1.compareTo(n2));
                   System.out.println(n2 + " compared to " + n1 + " returns "
                                      + n2.compareTo(n1));
                   // parsing a string
                   System.out.println("The string holds int value: "
                                      + Integer.parseInt("245"));
                   System.out.println("The string holds int value: "
                                      + Integer.parseInt("FF", 16));
                   System.out.println("The string holds int value: "
                                      + Integer.parseInt("100", 8));
                   System.out.println("The string holds int value: "
                                      + Integer.parseInt("Jim", 27));
             }
}
Output

Here is the program output:

n1 holds value: 5 
n2 holds value: 10
5 = 10 is false
5 compared to 10 returns -1
10 compared to 5 returns 1
The string holds int value: 245
The string holds int value: 255
The string holds int value: 64
The string holds int value: 14359



Updated 31-Mar-2019

Leave Comment

Comments

Liked By