< Prev
Next >

# Integer Wrapper Class

Integer wrapper class is used to create an object version of a primitive int value.

## Constructor of Integer wrapper class

Constructor Description
Integer(int i) Constructor of Integer wrapper class takes a primitive int value.
Integer(String str) Constructor of Integer wrapper class also takes a String equivalent of a primitive int value.

## Creating an Integer object

``````class A
{
public static void main(String... ar)
{
Integer b1 = new Integer(10); 		//Passing a primitive int value.
Integer b2 = new Integer("20"); 	//Passing primitive int as a String.
}
}``````

## Reading an Integer wrapper class object.

There are two ways to read an Integer object-
• By directly printing the Integer object.
• Calling method, intValue() of Integer class, which returns the primitive int equivalent.
``````class A
{
public static void main(String... ar)
{

Integer i1 = new Integer(10); 		//Boxing primitive int in an Integer object.
Integer i2 = new Integer("20"); 	//Passing a primitive int as a String

//Unboxing an Integer object.
System.out.println("int value in an Integer object, i1 : "+ i1);

//Unboxing an Integer object using intValue() method
System.out.println("int value in an Integer object, i2 : "+ i2.intValue());
}
}```
```

## Output-

``````int value in an Integer object, i1 : 10
int value in an Integer object, i2 : 20``````

## Some important methods of Integer wrapper class

Methods Description
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 a Integer object after converting it from a String, s.
short shortValue() Converts a Integer object to a primitive short value and returns it.
byte byteValue() Converts an Integer object to a primitive byte value and returns it.
int intValue() Converts an Integer object to a primitive int value and returns it
long longValue() Converts an Integer object to a primitive long value and returns it
float floatValue() Converts an Integer object to a primitive float value and returns it
double doubleValue() Converts an Integer object to a primitive double value and returns it.

## Using compareTo() method to compare values in two Integer objects.

Method compareTo(Integer i) takes Integer class type object and it -
• Returns a zero if the invoked Integer object contains the value same as i.
• Returns 1 if the invoked Integer object contains value larger than i.
• Returns -1 if the invoked Integer object contains value smaller than i.
``````class A
{
public static void main(String... ar)
{
Integer i1 = new Integer("10"); //Constructor accepting String value
Integer i2 = new Integer(10);	  //Constructor accepting primitive int value

System.out.println("Value in i1 = "+ i1);
System.out.println("Value in i2 = "+ i2);

System.out.println("Invoking i1 to compare with i2 : "+ i1.compareTo(i2));

Integer i3 = new Integer("11"); //Passing primitive int as a String to Constructor
Integer i4 = new Integer(20);  //Passing a primitive int directly to Constructor

System.out.println("Value in i3 = "+i3);
System.out.println("Value in i4 = "+i4);

System.out.println("Invoking i3 to compare with i4 : "+ i3.compareTo(i4));

System.out.println("Invoking i4 to compare with i3 : "+ i4.compareTo(i3));

}
}``````

## Output-

``````Value in i1 = 10
Value in i2 = 10
Invoking i1 to compare with i2 : 0
Value in i3 = 11
Value in i4 = 20
Invoking i3 to compare with i4 : -1
Invoking i4 to compare with i3 : 1``````

## To convert a String to a primitive int value using parseInteger() method.

Method parseInteger(), takes a string value and parse it to primitive int value.
``````//Converting String to primitive int value.

import java.util.*;

class A
{
public static void main(String... ar)
{
int b1 = Integer.parseInteger("20");
int b2 = Integer.parseInteger("-200");

System.out.println("Primitive int value in b1 : "+ b1);
System.out.println("Primitive int value in b2 : "+ b2);
}
}

``````

## Output-

``````Primitive int value in b1 : 20
Primitive int value in b2 : -200``````

## Exception while converting a String to an Integer object

NumberFormatException is raised when we pass String value to the constructor of Integer class or its method parseInteger(), which is not a legal int value or it is out-of-range.
``````//Converting String to object of Integer class by passing it to constructor of Integer class.

class A
{
public static void main(String... ar)
{
Integer b1 = new Integer("100t"); //Passing a String value that cannot be an Integer object.
}
}
``````

## Output -

``````Exception in thread "main" java.lang.NumberFormatException: For input string: "1
00t"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.(Unknown Source)
at A.main(Byte8.java:7)``````

## Exception when calling parseInt() method

``````//Converting String to primitive int value by passing it to  parseInt() method of Integer class.

class A
{
public static void main(String... ar)
{
int i = Integer.parseInt("-27f"); //Passing a String value that can't be converted to an int.
}
}
``````

## Output -

``````Exception in thread "main" java.lang.NumberFormatException: For input string: "-27f"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at A.main(Int3.java:7)``````

## Converting the value stored in a Byte object to all numeric primitive types.

``````import java.util.*;

class A
{
public static void main(String... ar)
{
Integer i = new Integer(10);	//Converting an int value argumet to wrapper Integer object

System.out.println("Value in wrapped Integer object, i : "+ i);

byte b = i.byteValue();		//Returns a primitive byte value out of a wrapped Integer object
short s= i.shortValue();	//Returns a primitive short value out of a wrapped Integer object
int i = i.intValue();		//Returns a primitive int value out of a wrapped Integer object
long l = i.longValue();		//Returns a primitive long value out of a wrapped Integer object
float f = i.floatValue();	//Returns a primitive float value out of a wrapped Integer object
double d = i.doubleValue();	//Returns a primitive double value out of a wrapped Integer object

System.out.println("byte value of Integer object, i : " + b);
System.out.println("short value of Integer object, i: " + s);
System.out.println("int value of Integer object, i : " + i);
System.out.println("long value of Integer object, i : " + l);
System.out.println("float value of Integer object, i : " + f);
System.out.println("double value of Integer object, i : "+ d);

}
}
``````

## Output-

``````Value in wrapped Integer object, i : 10
byte value of Integer object, i : 10
short value of Integer object, i : 10
int value of Integer object, i : 10
long value of Integer object, i : 10
float value of Integer object, i : 10.0
double value of Integer object, i : 10.0``````