All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Integer Wrapper Class In Java

Last Updated : Mar 11, 2024

Integer Wrapper Class In Java

In this article we will show you the solution of integer wrapper class in java, Java's Integer wrapper class is a important element that offers a means of encapsulating and manipulating basic integer values as objects.

It acts as a link between the worlds of object-oriented and basic programming, enabling programmers to take advantage of both worlds' advantages.

Java's primitive types, such as int, are not considered objects and do not have the functionality of object-oriented programming.

However, in some situations, such as when using collections or giving values to functions that anticipate objects, it is vital to treat integers as objects.

The Integer wrapper class is useful in this situation. The Integer class encapsulates the int value and offers a variety of helpful methods for carrying out different operations on integers, including as conversion, comparison, parsing, including arithmetic computations.

It enables programmers to use integer values while utilising the extensive capability of objects. We'll talk about the Java integer wrapper class now.

Step By Step Guide On Integer Wrapper Class In Java :-

import java.util.ArrayList;
import java.util.List;
public class IntegerWrapperExample {
    public static void main(String[] args) {
        // Creating Integer objects
        Integer num1 = new Integer(10);
        Integer num2 = new Integer("20");
        // Converting int to Integer
        int num3 = 30;
        Integer num4 = Integer.valueOf(num3);
        // Converting String to Integer
        String strNum = "40";
        Integer num5 = Integer.parseInt(strNum);
        // Auto-boxing: Converting int to Integer
        int num6 = 50;
        Integer num7 = num6;
        // Auto-unboxing: Converting Integer to int
        Integer num8 = new Integer(60);
        int num9 = num8;
        // Arithmetic operations with Integer objects
        Integer sum = num1 + num2;
        Integer difference = num4 - num5;
        Integer product = num6 * num7;
        Integer quotient = num8 / num9;
        Integer remainder = num3 % num4;
        // Integer comparison
        boolean isEqual = num1.equals(num2);
        boolean isGreater = num1 > num2;
        boolean isLessOrEqual = num4 <= num5;
        // Integer to String conversion
        String strNum2 = num9.toString();
        // Creating a List of Integer objects
        List<Integer> numbers = new ArrayList<>();
        numbers.add(num1);
        numbers.add(num2);
        numbers.add(num3);
        numbers.add(num4);
        numbers.add(num5);
        // Iterating over the List
        for (Integer number : numbers) {
            System.out.println(number);
        }
    }
}
  1. You can see that we wrote the Java code in this example to demonstrate the Java integer wrapper class.
  2. The first line of code imports the ArrayList and List classes from the java.util package. These classes are necessary for later in our code when we create a list of integer objects.
  3. After that, we create a class called IntegerWrapperExample, which houses our program's main method.
  4. We show many uses of the Integer wrapper class inside the main method:
  5. The valueOf method and the constructor are both used to create Integer objects. In this instance, we produce num1 with the value 10 and parse a string to construct num2 with the value 20.
  6. Using the valueOf method, we change the primitive int value num3 into an integer object num4.
  7. Using the parseInt function, we convert the textual value "40" to an Integer object num5.
  8. By automatically transforming the primitive int value num6 into an integer object num7, we demonstrate auto-boxing.
  9. By automatically transforming the integer object num8 to the primitive int value num9, we can demonstrate auto-unboxing.
  10. We use Integer objects to carry out arithmetic operations. We determine and keep in corresponding integer variables the sum, difference, product, quotient, and remainder of various integer objects.
  11. Integer objects are compared using techniques like equals. We use boolean variables to hold the outcomes of these comparisons.
  12. We use the toString function to convert the Integer object num9 to a String as an example of how to convert an integer to a string.
  13. Using an ArrayList, we produce a list of Integer objects with the name numbers. We use the add method to add a number of Integer objects to the list.
  14. Finally, we use a for-each loop to repeatedly cycle through the list, printing each Integer element using System.out.println.

Conclusion :-

As a result, we have successfully learned about Java's integer wrapper class.

We also discovered how effective the Java Integer wrapper class is in enabling us to work with integer values as objects.

It fills the gap between object-oriented programming and the realm of primitives.

Integer objects give us access to a large variety of practical ways for manipulating and applying operations to integer values.

I hope this article on integer wrapper class in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪