Java Wrapper Class: Integer

Get a brief introduction to Integer, a Java wrapper class.

Introducing Integer

The Integer class is part of the java.lang package.

The Integer class is a wrapper class for the primitive type int. It contains several methods to effectively deal with an integer. An object of an Integer class can hold a single int value.

Note: To get an overview of what a wrapper class is, check out this shot.

Creating an Integer object

This class has two constructors:

  • Integer (int value)
  • Integer (String string)

Let’s cover them one by one.

The Integer (int value) constructor

This constructor constructs a new Integer object that represents the specified int value.

Look at the program below.

Press + to interact
class IntegerWrapperDemo1
{
public static void main( String args[] )
{
Integer x = new Integer(5); // Creating an Integer type object
System.out.println( x );
}
}

Look at line 5. We create the Integer object: x. We pass 55 as the argument. Now when we try to print x, we get 55 ...