In this article, we’ll have a comprehensive look at the Java integer class, a fundamental component of Java programming. The Integer class in Java simplifies working with integer values by providing methods for conversion, arithmetic operations, and comparisons. So let’s get started
Introduction
In Java, the Integer class is one of the wrapper classes. Wrapper class helps to access primitive datatypes as objects. This class belongs to ‘ java. lang.Integer package. This package is automatically imported into every Java program i.e. we do not need to explicitly import this package.
Integer class objects
The objects in the ‘Integer’ class are immutable, which means once the objects are created they cannot be changed. Immutability means that once an object is created, its state cannot be modified. Due to the immutability of an object java offers several advantages such as:
-
-
- it reduces the risk of concurrent modification, which promotes thread safety.
- These objects can be shared among multiple objects.
- It makes code debugging easy because the behavior of objects is predictable.
- it promotes code readability and interpretability.
- it avoids synchronization issues.
-
Data handling and conversion
Java supports automatic conversion between primitive types like int, and their corresponding wrapper classes using the concept of auto-boxing and unboxing. This feature in Java makes it easier to work with primitive type and their corresponding wrapper class
Auto-boxing helps to convert ‘int‘ to the wrapper class ‘Integer‘. Check the below example:
int primitive_int = 10; Integer wrapper_int = primitive_int; //Auto-boxing
Whereas unboxing converts ‘Integer‘ to primitive type ‘int‘. Check the below example:
Integer wrapper_int = 20; int primitive_int = wrapper_int; //Unboxing
Methods of Integer Class
Java Integer class provides a wide range of methods and functionalities that can be accessed by wrapping primitive ‘int‘ values into objects within the class. Some methods include:
Examples
Here’s a simple Java program that creates ‘Integer‘ objects and prints their values:
Printing Integers using Constructors:
public class IntegerExample { public static void main(String[] args) { // Creating Integer objects using constructors Integer num1 = new Integer(10); Integer num2 = new Integer("20"); System.out.println("Value of num1: " + num1); System.out.println("Value of num2: " + num2); } }
Output: Value of num1: 10 Value of num2: 20
Summing Two Integers:
class IntegerExample { public static void main(String[] args) { //addition two integers using default method sum(). int sum = Integer.sum(10, 20); System.out.println("addition of two integers: " + sum); } }
Output: addition of two integers: 30
Comparing Integers:
class IntegerExample { public static void main(String[] args) { // Comparing two Integer objects numerically Integer num1 = 80; Integer num2 = 80; int result1 = num1.compareTo(num2); Integer num3 = 45; Integer num4 = 89; int result2 = num3.compareTo(num4); //printing the comparision result System.out.println("Comparison result of num1 and num2: " + result1); System.out.println("Comparison result of num3 and num4: " + result2); } }
Output: Comparison result of num1 and num2: 0 Comparison result of num3 and num4: -1