Working with the Integer Class in the Java Programming Language

java treemapThe Integer class in Java belongs to the java.lang package, which means any class is free to use the methods in this class without importing any package. It implicitly extends the class Number, which is a direct subclass of Object. Number is an abstract class and acts as a superclass to BigInteger, BigDecimal, and most wrapper classes — Double, Integer, Short, Byte, Float and Long. The Number class implements the interface Serializable.

New to Java programming? Learn about the various features of Java through a course at Udemy.com

The Integer class is a wrapper class. Its main function is to wrap the int primitive data type into an object. An Integer object has only one variable of type int. The class provides a series of methods that can be useful to work with an int data type. A most commonly used feature of the Integer class is to convert an int to a String and vice versa. It implements two Interfaces: Serializable and Comparable<Integer>.

Wrapping and Unwrapping an int Variable

Converting an int to a string representation of the integer value using Integer Class:

Class covertToString

{

Public static void main(String[] arg)

{

int x = 345;        // declare an int variable

Integer y = new Integer(x);  // wrap the int variable in an Integer object

String s = Integer.toString(y);

System.out.println(s);

}

}

OUTPUT:  345

345 is now a string representation of the int variable x;

Convert a String to an integer value using Integer class:

class convertToint

{

public static void main(String[] arg)

{

String s = “35684”;   // declare a string variable

Integer y = Integer.parseInt(s); // the parseInt method converts a String object to an Integer object

int x = y.intValue(); //the Integer object y is converted to an int variable x using the intValue method of the Integer class

System.out.print(x);

}

}

OUTPUT:

35684

The output is now an int value.

The above two examples demonstrate the wrapping and unwrapping of an int variable.

This is mostly useful when you want to pass int variables as items of a Collection, such as an Arraylist or List. With Java 5.0 and later versions, however, the wrapping and unwrapping is automatically achieved using a concept called autoboxing; you can treat an int and an Integer as if they were the same thing.

Java int vs Java Integer

The int data type is a primitive data type. An int variable stores the actual binary value of the integer it is assigned. In the declaration of int x = 564, the variable x stores the binary value of the integer number 564. An int variable can store 4 bytes of data, which means the value of an int can hold range between -2,147,483,648 and +2,147,483,647.

Integer is a class in Java. The Integer variable stores a reference to an Integer object.  Java Integer has various static methods that you can use in your class. Since the methods are static, you do not need an Integer object to access the methods. In the above examples, the methods parseInt and toString were called directly using the class name Integer and not an object of the class Integer. The Integer class is declared as follows:

public final class Integer extends Number implements Comparable<Integer>

Constructors of Java Integer Class:

A constructor has code that run every time you create an object of a class, or when the class is loaded. It looks and works a lot like a method but it has no return type. A distinctive property of a constructor is that it has the same name as the class. The Java JVM implicitly provides a default constructor for every class. If a class explicitly declares one or more constructors, then no default constructor is created.

Learn about various Java concepts through a course at Udemy.com

The Integer class has two constructors:

public Integer(int value)

When the Integer class loads, this constructor allocates an Integer object for the int value that is passed as argument. In Java 5.0 and later, the integer object can be directly assigned to an int variable.

Example:

Integer x = new Integer(2574);  // create a new Integer object by passing integer value

int y = x.intValue();      // assign value to an int variable

Or

int y = new Integer(2574);
public Integer(String value)

This constructor allocates an Integer Object that represents the number value of the argument passed to the constructor. The argument must be a textual representation of an integer value.

Example: Integer sx = new Integer(“6734”); // create new Integer object by passing a parsable integer value

int sy = sx.intValue();

Or

int sy =  new Integer(“6734”);

This constructor throws a NumberFormatException if the argument is not a parsable integer, which means it’s not a textual representation of an integer value. Let’s look at the following example.

class Con

{

Public static void main(String[] arg)

{

Integer const = new Integer(“456r”);

int cy = const.intValue();

System.out.println(cy);

}

}

The above codes would give the following error:

Exception in thread “main” java.lang.NumberFormatException: For input string “456r”. This error triggers because the “r” in the variable makes the number invalid.

Fields in Java Integer Classes:

All fields in the Integer class are declared as static. This means there is exactly just one copy of the variables through all objects of the Integer class.

There are four fields in the Integer class.

1)     static int MIN_VALUE – this is a constant variable that holds the minimum value an int can have, which is -231.

2)     Static int MAX_VALUE – this is a constant variable that holds the maximum value an int can have, which is 231 – 1.

3)     Static Class<Integer> TYPE – this is a class instance that represents the type int.

4)     Static int SIZE – This variable contains the number of bits that represents an integer number in 2’s complement form.

Learn about various fields in different Java classes using a course at Udemy.com.

Some Methods in the Java Integer Class:

public double doubleValue()

This method returns the value of an Integer object as a double.

Example:

Integer abc = new Integer(682);

double y = abc.doubleValue();

System.out.print(“y = “ + y);

OUTPUT:

y = 682.0

public boolean equals(Object ob)

This method compares the object passed as an argument to the object that calls this method. It is true if the argument passed is an Integer object that contains the same int value as the object it is compared against. Also, the Integer object should not be null.

Example:

Integer a = new Integer(65);

Integer b = new Integer(65);

System.out.println(“ Is object a equal to object b? “);

System.out.println(a.equals(b));

OUTPUT:

Is object a equal to object b? true

In the above example if the arguments passed to the Integer constructors were different, a.equals(b) would return false.