Search⌘ K

Java Wrapper Class: Integer

Explore the Java Integer wrapper class, its constructors, and key methods. Understand special values like MIN_VALUE and MAX_VALUE, and learn how overflow and underflow affect integer computations. This lesson helps you work confidently with Integer objects in Java for effective programming and exam preparation.

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.

Java
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 ...