Table of contents
1.
Introduction
2.
What is Java Integer Class?
3.
Declaration
4.
Field for Java Integer Class
5.
Constructor
6.
Methods of Java Integer Class
6.1.
bitCount()
6.1.1.
Syntax
6.1.2.
Return value
6.1.3.
Example
6.1.4.
Output
6.2.
byteValue()
6.2.1.
Syntax
6.2.2.
Return Value
6.2.3.
Example
6.2.4.
Output
6.3.
compare()
6.3.1.
Syntax
6.3.2.
Return Value
6.3.3.
Example
6.3.4.
Output
6.4.
compareTo()
6.4.1.
Syntax
6.4.2.
Return Value
6.4.3.
Example
6.4.4.
Output
6.5.
compareUnsigned()
6.5.1.
Syntax
6.5.2.
Return Value
6.5.3.
Example
6.5.4.
Output
6.6.
equals()
6.6.1.
Syntax
6.6.2.
Return Value
6.6.3.
Example
6.6.4.
Output
6.7.
max()
6.7.1.
Syntax
6.7.2.
Return Value
6.7.3.
Example
6.7.4.
Output
6.8.
min()
6.8.1.
Syntax
6.8.2.
Return Value
6.8.3.
Example
6.8.4.
Output
6.9.
reverse()
6.9.1.
Syntax
6.9.2.
Return Value
6.9.3.
Example
6.9.4.
Output
6.10.
sum()
6.10.1.
Syntax
6.10.2.
Return Value
6.10.3.
Example
6.10.4.
Output
7.
Frequently Asked Questions
7.1.
What are the 4 types of integers in Java?
7.2.
What is a wrapper class in Java?
7.3.
What is the value of a to Z integer in Java?
8.
Conclusion
Last Updated: Dec 23, 2024

Java Integer Class

Author Aniket Majhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Java Integer class is a wrapper class that provides object representation and functionality for primitive integer values. It offers methods to convert integers to strings, parse strings into integers, and perform various arithmetic and bitwise operations. The Integer class also defines constants and static methods to work with int values effectively. It is part of the Java.lang package and extends the Number class. This article will discuss the Integer class and its different properties, along with examples and their code implementations.

Recommended Topic, Multithreading in javaDuck Number in Java

What is Java Integer Class?

The Java Integer class is a fundamental part of the Java programming language, and it serves as a wrapper class for the primitive int data type. It provides an object-oriented representation of integers, which allows them to be used in scenarios where objects are required, like collections or generics. The Integer class offers many methods for converting integers to strings, parsing strings into integers, and performing arithmetic and bitwise operations. It also defines useful constants, such as the minimum and maximum values for an int. Moreover, the Integer class provides static utility methods for working with integers, like comparing, dividing, and reversing their order. 

Declaration

The declaration of the Integer class is as follows:

public final class Integer extends Number implements Comparable<Integer>
You can also try this code with Online Java Compiler
Run Code

Field for Java Integer Class

The fields for the java Integer class are defined below:

  1. static int MAX_VALUE:  This is a constant that is holding the maximum value of the int can have, 231 - 1
  2. static int MIN_VALUE: This is a constant that is holding the minimum value of the int can have, -231
  3. static class<Integer> Type: This is the class instance representing the primitive type int.
  4. static int SIZE: This is the number of bits used to represent the int value in the 2’s complement binary form.
     

Also see,  Swap Function in Java

Constructor

The Java Integer class provides mainly two types of constructors, these are as follows:

  • Integer(int param): It accepts a parameter of type int and constructs a newly allocated Integer object that represents the specified int value.
int a = 45;
Integer x = new Integer(a);
You can also try this code with Online Java Compiler
Run Code
  • Integer(String s): It accepts a parameter of type String and converts that String into an Integer class object.
String b = "45";
Integer y = new Integer(b);
You can also try this code with Online Java Compiler
Run Code

Methods of Java Integer Class

Some of the most useful Java Integer class methods are as follows:

bitCount()

The method is described below:

Syntax

public static int bitCount(int i)
You can also try this code with Online Java Compiler
Run Code

Return value

It returns the number of 1’s in the 2’s complement of the binary representation of the integer value.

Example

public class IntBitCount{
    public static void main(String[] args) {
        int n = 19;
        int res = Integer.bitCount(19);
        System.out.printf("The bitCount for n = %d is %d",n , res);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

byteValue()

The method is described below:

Syntax

public byte byteValue()  
You can also try this code with Online Java Compiler
Run Code

Return Value

This method returns the numeric value represented by this object after conversion to type byte.

Example

public class IntByteValue {
    public static void main(String[] args) {
      Integer obj = new Integer(10);
      byte b = obj.byteValue();
      System.out.println("Value of b = " + b);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

compare()

The method is described below:

This method compares two integer values numerically.

Syntax

public static int compare(int x,int y)
You can also try this code with Online Java Compiler
Run Code

Return Value

If  x == y, it returns 0.

If x > y, it returns 1.

If x < y, it returns -1.

Example

public class IntByteValue{
    public static void main(String[] args) {
        int a = 10;
        int b = 10;
        int c = 5;
        int d = 15;

        int res1 = Integer.compare(a , b);
        int res2 = Integer.compare(a , c);
        int res3 = Integer.compare(a , d);
       
        System.out.println("The Compare result of a and b is: " + res1);
        System.out.println("The Compare result of a and c is: " + res2);
        System.out.println("The Compare result of a and d is: " + res3);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

compareTo()

The method is discussed below.

Syntax

public int compareTo(Integer anotherInteger)
You can also try this code with Online Java Compiler
Run Code

Return Value

If Integer == anotherInteger, it returns 0.

If Integer > anotherInteger, it returns 1.

If Integer < anotherInteger, it returns -1.

Example

public class IntCompareTo {
    public static void main(String[] args) {
        Integer a = new Integer(10);
        Integer b = new Integer(10);
        Integer c = new Integer(5);
        Integer d = new Integer(15);

        int res1 = a.compareTo(b);
        int res2 = a.compareTo(c);
        int res3 = a.compareTo(d);

        System.out.println("The compareTo result for a and b is: " + res1);
        System.out.println("The compareTo result for a and c is: " + res2);
        System.out.println("The compareTo result for a and d is: " + res3);

    }
}
You can also try this code with Online Java Compiler
Run Code

Output

compareUnsigned()

The method is discussed below:

Syntax

public static int compareUnsigned(int x,int y)  
You can also try this code with Online Java Compiler
Run Code

Return Value

If  x == y,it returns 0.

If x > y, it returns 1.

If x < y, it returns -1.

Example

public class IntCompareUnsigned {
    public static void main(String[] args) {
        int a = 10;
        int b = 10;
        int c = 5;
        int d = 15;

        int res1 = Integer.compareUnsigned(a , b);
        int res2 = Integer.compareUnsigned(a , c);
        int res3 = Integer.compareUnsigned(a , d);
       
        System.out.println("The Compare result of a and b is: " + res1);
        System.out.println("The Compare result of a and c is: " + res2);
        System.out.println("The Compare result of a and d is: " + res3);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

equals()

The method is discussed below.

Syntax

public boolean equals(Object obj)  
You can also try this code with Online Java Compiler
Run Code

Return Value

It returns true if the argument is not null and if the integer object is the same as the method argument object; otherwise, it will return false.

Example

public class IntEquals {
    public static void main(String[] args) {
        Integer a = new Integer(10);
        Integer b = new Integer(15); // a != b
        Integer c = new Integer(10); // a == c

        boolean res1 = a.equals(b);
        boolean res2 = a.equals(c);

        System.out.println("The Equals Result for a and b is: "+ res1);
        System.out.println("The Equals Result for a and c is: "+ res2);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

max()

The method is discussed below.

Syntax

public static int max(int x, int y)  
public static float max(float x, float y)  
public static long max(long x, long y)  
public static double max(double x, double y)
You can also try this code with Online Java Compiler
Run Code

Return Value

This method returns the max value among a and b.

Example

public class IntMax {
    public static void main(String[] args) {
        int ia = 10;
        int ib = 7;
        int res = Math.max(ia , ib);
        System.out.println("The max among ia and ib is: " + res);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Practice by yourself on online java compiler.

min()

The method is discussed below.

Syntax

public static int min(int x, int y)  
public static float min(float x, float y)  
public static long min(long x, long y)  
public static double min(double x, double y)
You can also try this code with Online Java Compiler
Run Code

Return Value

It returns the minimum value between a and b.

Example

public class IntMin {
    public static void main(String[] args) {
        int ia = 10;
        int ib = 10;

        int res = Math.min(ia , ib);
        System.out.println("The max among ia and ib is: " + res);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

reverse()

The method is discussed below.

Syntax

public static int reverse(int i)
You can also try this code with Online Java Compiler
Run Code

Return Value

It returns a numeric value that is obtained from the reversing of the bits in the specified integer argument.

Example

public class IntReverse {
   public static void main(String[] args) {
       int a = 10;
       int rev = Integer.reverse(a);
       System.out.println("The Reverse of a is: " + rev);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

sum()

The method is discussed below:

Syntax

public static int sum(int x, int y)  
public static float sum(float x, float y)  
public static long sum(long x, long y)  
public static double sum(double x, double y)
You can also try this code with Online Java Compiler
Run Code

Return Value

It returns the sum of a and b.

Example

public class IntSum {
  public static void main(String[] args) {
      int a = 10;
      int b = 10;
      int res = Integer.sum(a , b);
      System.out.println("The sum of a and b is: " + res);
  }  
}
You can also try this code with Online Java Compiler
Run Code

Output

Check out this problem - Two Sum Problem

Must Read: Java System Out Println

Frequently Asked Questions

What are the 4 types of integers in Java?

Java has four integer types: byte, short, int, and long. They differ in size and range, accommodating various data size requirements from small to large integer values.
 

What is a wrapper class in Java?

A wrapper class in Java is a class that is used to convert a primitive data type to an object data type.
 

What is the value of a to Z integer in Java?

In Java, the integer values of 'a' to 'z' correspond to their ASCII values, which range from 97 to 122. These are commonly used in character data processing.

Conclusion

In this article, we have extensively discussed the Java Integer class.

We started with the basic concept of the wrapper classes, then we moved to the integer class, its basic definition, different fields, types of constructors and different methods using examples.

We hope that this blog has helped you enhance your knowledge regarding Java Integer Class and if you would like to learn more, check out our articles on Java Float ClassJava Double Class. Do upvote our blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Live masterclass