Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
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.
Constructors | Description |
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 |
Method | Modifier and Type | Description |
bitCount(int i) | static int | Returns the number of one-bits in the two’s complement binary, representation of the specified int value. |
byteValue() | byte | Returns the value of this Integer as a byte. |
compare(int x, int y) | static int | Compares two int values numerically. |
compareTo(Integer anotherInteger) | int | Compares two Integer objects numerically. |
decode(String nm) | static Integer | Decodes a String into an Integer. |
doubleValue() | double | Returns the value of this Integer as a double. |
equals(Object obj) | boolean | Compares this object to the specified object. |
floatValue() | float | Returns the value of this Integer as a float. |
getInteger(String nm) | static Integer | Determines the integer value of the system property with the specified name. |
hashCode() | int | Returns a hash code for this Integer. |
intValue() | int | Returns the value of this Integer as an int. |
longValue() | long | Returns 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 Int | Returns 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 Int | Returns the value obtained by reversing the order of the bytes in the two’s complement representation of the specified int value. |
shortValue() | short | Returns the value of this Integer as a short. |
toString() | String | Returns 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 Integer | Returns an Integer instance representing the specified int value. |
valueOf(String s) | static Integer | Returns 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.
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 21st December,2024 21st December SAT&SUN (Weekend Batch) | View Details |
Java Course Online | Class Starts on 1st March,2025 1st March SAT&SUN (Weekend Batch) | View Details |
edureka.co