What is Integer class in java and how it works?

Published on Sep 25,2019 4.4K Views

What is Integer class in java and how it works?

edureka.co

Java has a comprehensive collection of built-in Classes and Interfaces. Among them, one of the popularly used class is the Integer classes in Java, which is a part of the wrapper class for primitive type. In this blog, you will learn  all about integer classes in the following order:

Let’s begin.

What is an Integer class in Java?

An Integer class in Java wraps a value of the primitive type int in an object. An object of type Integer contains a single field, which is int type. The Java Integer class falls under the Java.lang.Number package. Here’s a complete hierarchy:

java.lang.Object
      java.lang.Number
           java.lang.Integer

Java Integer class contains various constructors and methods. Let’s directly look into them.

Java.lang.Integer class Constructor

ConstructorsDescription

Integer(int value)

Constructs newly allocated integer object with specified Int

integer(String s)

Constructs newly allocated object which represents Int value indicated by the parameter String

Java.lang.Integer class Methods

MethodModifier and TypeDescription
bitCount(int i)static intReturns the number of one-bits in the two’s complement binary, representation of the specified int value.
byteValue()byteReturns the value of this Integer as a byte.
compare(int x, int y)static intCompares two int values numerically.
compareTo(Integer anotherInteger)intCompares two Integer objects numerically.
decode(String nm)static IntegerDecodes a String into an Integer.
doubleValue()doubleReturns the value of this Integer as a double.
equals(Object obj)booleanCompares this object to the specified object.
floatValue()floatReturns the value of this Integer as a float.
getInteger(String nm)static IntegerDetermines the integer value of the system property with the specified name.
hashCode()intReturns a hash code for this Integer.
intValue()intReturns the value of this Integer as an int.
longValue()longReturns the value of this Integer as a long.
lowestOneBit(int i)static Int
Returns an int value with at most a single one-bit, in the position of the lowest-order (“rightmost”) one-bit in the specified intvalue.
reverse(int i)static IntReturns the value obtained by reversing the order of the bits in the two’s complement binary representation of the specified int value.
reverseBytes(int i)static IntReturns the value obtained by reversing the order of the bytes in the two’s complement representation of the specified int value.
shortValue()shortReturns the value of this Integer as a short.
toString()StringReturns a String object representing this Integer’s value.
toString(int i)static String
Returns a String object representing the specified integer.
valueOf(int i)static IntegerReturns an Integer instance representing the specified int value.
valueOf(String s)static IntegerReturns an Integer object holding the value of the specified String.

You can know more about these methods here.
Now that you know the different methods used in  Integer class, it’s time we implement some of its major methods.

Java Integer Examples

In this section, I have implemented the first five methods used in the “integer class in Java”. Similarly, you can implement the rest of them. Do let me know if you face any difficulty. Refer the reference code below:


package Edureka;

import java.io.*; 
import java.util.*; 

public class javaIntegerExamples{    
	    public static void main(String args[])  
	    { 
	    		 int value = 161;
	    		 // Get the binary equivalent
	    		 System.out.println("Binary equivalent:"+Integer.toBinaryString(value));
	    		 System.out.println("Bit Count:"+Integer.bitCount(value));

	    		 //example for byteValue()
	    		 int Value1=123;
	    		 Integer a = new Integer(Value1);
	    			System.out.println("Byte Value is "+a.byteValue());
	    			
	    		//compare two integer values
	    			System.out.println(Integer.compare(20, 20));
	    			System.out.println(Integer.compare(20, 19));
	    			System.out.println(Integer.compare(20, 22));
	    			
	    		//compare two integers
	    			Integer value2 = new Integer(50);
	    			System.out.println(value2.compareTo(50)); 
	    			System.out.println(value2.compareTo(49));
	    			System.out.println(value2.compareTo(51));
	    			
	    		//decode the string
	    			System.out.println(Integer.decode("0124")); //base8
	    			System.out.println(Integer.decode("0x124")); //base16
	    		 }
	    		 
	    }

Output:

Binary equivalent:10100001
Bit Count:3
Byte Value is 123
0
1
-1
0
1
-1
84
292

This brings us to the end of this article where we have understood Integer Class in Java. Hope you guys are clear with this topic.

If you found this article on “Java Integer class” relevant, check out the Edureka Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you come across any questions, feel free to ask all your questions in the comments section of this blog and our team will be glad to answer.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework