Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Definition
3.
Declaration
4.
Field
5.
Constructor
6.
Methods
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.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

Java Integer Class

Author Aniket Majhi
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Welcome readers!!

This blog is going to be interesting for you. 

If you have a basic knowledge about wrapper classes in Java, then this blog will be helpful for you to learn one of the wrapper classes available in Java, namely the Integer Class. You will learn about the Integer class and their different properties with good examples and code implementations through this blog.

So without further ado, let’s start the blog.

Recommended Topic, Multithreading in javaDuck Number in Java

Definition

The Java Integer class is a wrapper class used to wrap the value of the primitive int data type to an object data type. The object only contains a single field of type int. It includes several methods to deal with the int values.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Declaration

The declaration of the Integer class is as follows:

public final class Integer extends Number implements Comparable<Integer>

Field

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);
  • 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);

Methods

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)

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);
    }
}

Output

byteValue()

The method is described below:

Syntax

public byte byteValue()  

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);
    }
}

Output

compare()

The method is described below:

This method compares two integer values numerically.

Syntax

public static int compare(int x,int y)

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);
    }
}

Output

compareTo()

The method is discussed below.

Syntax

public int compareTo(Integer anotherInteger)

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);

    }
}

Output

compareUnsigned()

The method is discussed below:

Syntax

public static int compareUnsigned(int x,int y)  

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);
    }
}

Output

equals()

The method is discussed below.

Syntax

public boolean equals(Object obj)  

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);
    }
}

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)

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);
    }
}

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)

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);
    }
}

Output

reverse()

The method is discussed below.

Syntax

public static int reverse(int i)

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);
   }
}

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)

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);
  }  
}

Output

Check out this problem - Two Sum Problem

Must Read: Java System Out Println

FAQs

  1. What is an Integer class in Java?
    The java Integer class is a wrapper class used to wrap the value of the primitive int data type to an object data type.
     
  2. 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.
     
  3. Is String a wrapper class in Java?
    No String is not a wrapper class in Java.
     
  4. Is boolean a wrapper class in Java?
    A wrapper class in Java named Boolean wraps the primitive data type boolean.

Key Takeaways

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.!

Happy Reading!

Previous article
Java Short Class
Next article
Java Long Class
Live masterclass