Wrapper classes in Java

Posted on

Wrapper classes: Java provides a rich set of wrapper classes. Wrapper classes are used to convert primitive data.

Integer class: A wrapper around the data type “int”.

Constructor:

  1. Integer(int num); Constructs an integer object from an integer variable.
  2. Integer(String str); Constructs an “Integer” object from a “String” object.
import java.lang.*;
class App50
{
public static void main(String yck[])
{
int z;
z=10;
Integer x=new Integer(z);
String s=new String("200");
Integer y=new Integer(s);
System.out.println(x);
System.out.println(y);
}
}


output:

\>javac App50.java
\>java App50
10
200

Methods:

  1. byte byteValue(); Converts to byte.
  2. double doubleValue(); Converts to double.
  3. float floatValue(); Converts to float.
  4. int intValue(); Converts to int.

  5. long longValue(); Converts to long.
  6. short shortValue(); Converts to short.
  7. String toString(); Converts to String.
import java.lang.*;
class App51
{
public static void main(String yck[])
{
int x;
x=10;
Integer i1=new Integer(x);

byte b;
b=i1.byteValue();
System.out.println("byte equivalent :"+b);

double d;
d=i1.doubleValue();
System.out.println("double equivalent :"+d);

float f;
f=i1.floatValue();
System.out.println("float equivalent :"+f);

int p;
p=i1.intValue();
System.out.println("int equivalent :"+p);

long l;
l=i1.longValue();
System.out.println("long equivalent :"+l);

short s;
s=i1.shortValue();
System.out.println("short equivalent :"+s);

String s1;
s1=i1.toString();
System.out.println("String equivalent :"+s1);
}
}


output:

\>javac App51.java
\>java App51
byte equivalent :10
double equivalent :10.0
float equivalent :10.0
int equivalent :10
long equivalent :10
short equivalent :10
String equivalent :10

Static StringtoBinaryString(int num); Converts to binary format.

Static StringtoHexString(int num); Converts to hexadecimal format.

Static StringtoOctalString(int num); Converts to octal format.

Note: “Static” methods of the class can be invoked directly with the name of the class. To use them you need not create objects.

    import java.lang.*;
    class App52
    {
    public static void main(String yck[])
    {
    System.out.println(Integer.toBinaryString(10));
    System.out.println(Integer.toHexString(10));
    System.out.println(Integer.toOctalString(10));
    }
    }
    
    
    output:
    
    \>javac App52.java
    \>java App52
    1010
    a
    12
    
    
    Static int.parseInt(String str); Converts the contents of the object 'str' into 'int' data type.
      import java.lang.*;
      class App53
      {
      public static void main(String yck[])
      {
      String s1=new String("100");
      String s2=new String("200");
      int sum=Integer.parseInt(s1)+Integer.parseInt(s2);
      System.out.println("The sum of two strings is : "+sum);
      }
      }
      
      
      output:
      
      \>javac App53.java
      \>java App53
      The sum of two strings is : 300

      boolean equals(object integerobject); Returns true if the invoking integer object is equivalent to integerobject.

      int compareTo(integer); Compares the numerical value of the invoking object with that of given integers.