×

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for converting an Integer to a String or vice versa. This class also provides other constants and methods which are useful when we are dealing with int.
Methods Description
bitCount() It returns the number of one bits in two’s complement binary representation of the given int value.
byteintValue() It returns the value of Integer as a byte.
compare() It compares the two int values.
compareTo() It compares two Integer objects numerically.
compareUnsigned() It compares the two int values keeping the values unsigned.
decode() It decodes a string into an Integer.
divideUnsigned() It returns the unsigned quotient by dividing the first argument by the second.
doubleValue() IT returns the value of Integer as a double.
equals() It compares this object with the specified object.
floatValue() It returns the float type value for the given Integer object.
getInteger() It determines the int value of the system property for the specified name.
hashCode() It returns the hash code for the specified Integer object.
highestOneBit() It returns an int value with at most one single bit in the place of the highest order one-bit.
intValue() It returns an int value for this Integer.
longValue() It returns a long value for this Integer object
lowestOneBit() It returns an int value with at most one single bit in the place of the lowest order one-bit.
max() It returns the greater of the two int values.
min() It returns the smaller of the two int values.
numberOfLeadingZeros(int i) It returns the total number of zero bytes following the highest order one-bit in the two’s complement binary representation.
numberOfTrailingZeroes(int i) It returns the total number of zero bytes following the lowest order one-bit in the two’s complement binary representation.
parseInt() It parses the CharSequence or String argument as a specified signed int.
parseUnsignedInteger() It parses the given CharSequence or String argument as an unsigned int.
remainderUnsigned() It returns the unsigned remainder by dividing the first argument with the second argument.
reverse() It returns the value obtained by the reversing the given order of bits in 2’s complement binary representation.
reverseBytes() It returns the value obtained by the reversing the given order of bytes in 2’s complement representation.
rotateLeft() It returns the value obtained by rotating the 2’s complement binary representation of the given int value by left and by the specified number of bits.
rotateRight() It returns the value obtained by rotating the 2’s complement binary representation of the given int value by right and by the specified number of bits.
shortValue() It returns the short type value for the given Integer object.
signum() It returns the signum function for the given int value.
sum() It adds the two specified int values and returns the same result as the + operator.
toBinaryString() It returns the string representation for the given integer argument as an unsigned argument with base 2.
toHexString() It returns the string representation for the given int argument as an unsigned argument with base 16.
toOctalString() Returns the string representation for the given int argument as an unsigned argument with base 8.
toString() It returns the string which represents the same value as of the given integer.
toUnsignedString() It returns the string representation for the argument as an unsigned decimal value.
toUnsignedLong() It converts the given int argument to long by an unsigned conversion.
valueOf() It returns an int instance which represents the specified value.
Examples- Example:1
public class JavaIntegerExample1 {
    static int i=1;
    public static void main(String[] args) {
        Integer val1 = 500 ;
        Integer val2 = 127654 ;
        Integer val3=127654;
       // It compares two Integer objects numerically
        int val = val1.compareTo(val2);
        if (val>0) {
            System.out.println(i++ + ". "+val1 + " is greater than " + val2);
        }
        else{
            System.out.println(i++ + ". "+val2 + " is greater than " + val1);
        }
        //It is used check whether both int values are equal or not.
        Boolean b1 = val3.equals(val2);
        if (b1) {
            System.out.println(i++ + ". "+val2 + " and " + val3 +" are equal
.");
        }
        else{
            System.out.println(i++ + ". "+val1 + " and " + val2 +" are not equal
.
");
        }
        //returns the value of this Integer as a long
        Long f3 = val2.longValue();
        System.out.println(i++ + ". "+"Long value of "+val2+ " is : "+f3);
        //Returns a string representation of the Integer’s object
        String f4 = val2.toString();
        System.out.println(i++ + ". "+"String value of "+val2+ " is : "+f4);
       //It returns a double value for this Integer object
        Double f5 = val1.doubleValue();
        System.out.println(i++ + ". "+"Double value of "+val1+ " is : "+f5);
    }
}
Output:
500 is greater than 12
12 and 12 are equal.
Long value of 12 is : 12
String value of 12 is : 12
Double value of 500 is : 500.0
Example:2
public class JavaIntegerExample2 {
    static int i=1;
    public static void main(String[] args) {
        Integer val1 = 50 ;
        Integer val2 = 607 ;
        //compares the two specified int values
        int val = Integer.compare(val1,val2);
        if (val>0) {
            System.out.println(i++ + ". "+val1 + " is greater than " + val2);
        }
        else{
            System.out.println(i++ + ". "+val2 + " is greater than " + val1);
        }
        // returns the hash code of int value val1
        int f1 = val1.hashCode();
        System.out.println(i++ + ". "+"Hash code value of "+val1+ " is : "+f1);
       //returns the value of this Integer as a Float
        Float f2 = val2.floatValue();
        System.out.println(i++ + ". "+"Float value of "+val2+ " is : "+f2);
        //returns the value of this Integer as a Float
        Integer f3 = val2.intValue();
        System.out.println(i++ + ". "+"Integer value of "+val2+ " is : "+f3);
        //decodes a String into a Integer
        String str="123";
        Integer f4 = Integer.decode(str);
        System.out.println(i++ + ". "+"Decoded value of string value "+str+ " is
: "+f4);
    }
}
Output: 
607 is greater than 50
Hash code value of 50 is : 50
Float value of 607 is : 607.0
Integer value of 607 is : 607
Decoded value of string value 123 is : 123

Related Topics

Comparetoignorecase in Java

In Java, the function compareToIgnoreCase ( ) is part of the String class, which is part of the java.lang package. It is used to compare any two strings while disregarding...

4 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.

JRE (Java Runtime Environment)

JRE is an installation package that provides an environment to run the Java program on any Operating System. It does not deal with the development process of any application. It is a part...

2 minutes read.

Level order Traversal of a Binary Tree in Java

In Java, a level order traversal of a tree structure is also referred to as the breadth-first traversal of the binary tree. Regarding the subsequent binary tree: Level order traversal is as...

5 minutes read.

Blockchain in Java

Blockchain is a continuously expanding ledger that maintains an immutable, secure, and chronological record of all transactions that have ever occurred. It can be utilized to securely transfer money, assets,...

9 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

Java SE vs EE

Java : Java is an independent platform. It works on any kind of operating system. We use java to develop and to focus on large or major projects. The goal of...

3 minutes read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.

Longest Odd Even Subsequence in Java

In order to solve the Java problem known as the longest odd-even subsequence, one must identify a sequences in a non-negative array having size s that alternately includes odd and...

6 minutes read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

5 minutes read.

Class definition in Java

The class definition in Java Java is an object-oriented programming language. We essentially know that programming languages based on object-oriented paradigms have classes and objects in their concepts as main, which...

6 minutes read.

Get yesterdays date by no of days in Java

In this tutorial, we are going to learn how to get yesterday’s date by the no of days in Java. Using the Calendar class, one can get the current date....

1 minute read.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

Java Keywords

Java Keywords The particular words which are used in java programming language that act like a key or important words to write a code are called java keywords. Java Keywords are...

4 minutes read.

Java Append Data to File

When writing data to a file using the classes within the java.io package, its file will often be overwritten, meaning that any existing data will be removed and new data...

4 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.

Armstrong Number Program in Java

Armstrong Number Program in Java: A positive number is called an Armstrong number if the sum of the cube of each digit is equal to the number itself. There are...

6 minutes read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

Collections in Java

Collection framework is one of Java's most powerful subsystems. It is a set of classes and interfaces that is all-the-rage technology for superintending objects and a group of objects. In...

6 minutes read.