×

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

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

Bad Operand types for Binary Operator Java

We will discuss how to handle issues in bad operand types for binary operator &, bad operand types for binary operator &&, bad operand types for binary operator ==, and...

4 minutes read.

How to Create a Generic List in Java?

Generics are types that have parameters. The goal is to make it possible for methods, classes, and interfaces to take type (Integer, String, etc., and user-defined types) as a parameter....

4 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

For Loop Program in Java

For Loop Program in Java The for-loop program in Java is written when we want a particular set of statements to be executed repeatedly until the given criteria/ condition is met....

8 minutes read.

Default Virtual Behaviour in C++ vs Java

Virtual Behaviour in C++: The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual. The virtual class can be...

3 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

Java Integer toHexString() method

The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Java Pi

What is Pi? There are many formulas in geometry that employ the Pi constant to calculate things like circumference, area, and volume. A circle's circumference divided by its diameter yields a...

3 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

Java 16

Java 16 is the most recent short-term incremental release, based on Java 15, and it was released on March 16, 2021. Records and sealed classes are just two of the...

11 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.