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 CodeField for Java Integer Class
The fields for the java Integer class are defined below:
- static int MAX_VALUE: This is a constant that is holding the maximum value of the int can have, 231 - 1
- static int MIN_VALUE: This is a constant that is holding the minimum value of the int can have, -231
- static class<Integer> Type: This is the class instance representing the primitive type int.
- 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 CodeMethods 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 CodeReturn 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 CodeOutput

byteValue()
The method is described below:
Syntax
public byte byteValue()

You can also try this code with Online Java Compiler
Run CodeReturn 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 CodeOutput

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 CodeReturn 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 CodeOutput

compareTo()
The method is discussed below.
Syntax
public int compareTo(Integer anotherInteger)

You can also try this code with Online Java Compiler
Run CodeReturn 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 CodeOutput

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 CodeReturn 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 CodeOutput

equals()
The method is discussed below.
Syntax
public boolean equals(Object obj)

You can also try this code with Online Java Compiler
Run CodeReturn 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 CodeOutput

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 CodeReturn 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 CodeOutput

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 CodeReturn 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 CodeOutput

reverse()
The method is discussed below.
Syntax
public static int reverse(int i)

You can also try this code with Online Java Compiler
Run CodeReturn 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 CodeOutput

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 CodeReturn 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 CodeOutput

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 Class, Java 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.!