×

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

How to Convert Binary to Decimal in Java

How to Convert Binary to Decimal in Java There are two methods to convert binary to decimal. Using Integer.parseInt() method Using user-defined logic Using Integer.parseInt() method The parseInt() is a static method of...

2 minutes read.

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language. Introduction Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs...

4 minutes read.

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

Perfect Number in Java

The concept of a perfect number in Java will be defined in this chapter, along with creating Program code that determine whether a specific number is perfect or not. Additionally,...

4 minutes read.

Block Swap Algorithm for array rotation in Java

An array and r, the rotation factor by which the array must be rotated, are both provided to us. We must then return the rotated array. A well-known and popular method is...

4 minutes read.

Sleep Time in Java

Sleep is amethod in java that is related to thread class and it is a concept of multithreading and is used to stop the execution of the current thread a...

4 minutes read.

Operators in Java

There is a good operator environment provided by Java. These operators are divided into four different groups i.e. arithmetic, bitwise, relational, and logical. There are other operators also available to...

7 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

JIT in Java

What is JIT ? JIT stands for " Just in Time Compiler ". It is called "Just in time" because it is called at the very last moment when the interpretation...

5 minutes read.

Java String replaceFirst() method

This method replace the first substring with user specified String. Syntax: public String replaceFirst(String Regexp, String Replacement) Parameter: Regexp : Regular Expression Replacement : the String which would replace found expression Return: a string Java String replaceFirst()...

1 minute read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Shallow copy in Java

Java's most important task is making a copy or clone of an object. In this part, we'll talk about shallow copies in Java and how to make them of Java...

4 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Java calculate age

In this section, we will create a Java program that calculates age from the given date of birth or current date. In order to get the date of birth from the current...

6 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Diamond problem in Java

The Diamond Problem in Java is connected to multiple inheritances. It is also referred to as the "deadly diamond dilemma" or even the "deadly diamond of death”. The solution for...

5 minutes read.

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14. Features in Java 14 Switch ExpressionText BlocksRecordsNull Pointer ExceptionsInstance ofPackaging ToolGarbage collectors 1.Switch Expressions Switch...

3 minutes read.