×

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

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

2 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

Arithmetic Operations on String in Java

Introduction Arithmetic, Relational, Bitwise, and Logical operators are all available in Java. Simple mathematical calculations are performed using Java arithmetic operators. Basic Arithmetic operators are considered in Java to be Addition,...

4 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

Root exception in java

Java: The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java...

3 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

Equilibrium Index of an Array in Java

An array's equilibrium index is the condition where the sum of items with lower and higher indices equals. For example, an array A=[-4,5 2,6,-5] where: a[0]=-4, a[1]=5, a[2]=2, a[3]=6, a[4]=-5 Now according...

4 minutes read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

7 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

3 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Properties Class in Java

Properties class is associated with Java since JDK 1.0, i.e. it is a legacy class. It is the subclass of Hashtable. It is used to maintain the lists of values in which...

5 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

String Declaration in Java

A string is a group of characters. In Java, the string can be treated as both class and datatype. In Java programming, the String class have many advantages. Everything in...

3 minutes read.

How to Change the Day in the Date using Java?

To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a...

4 minutes read.

Bin Packing Problem Java

Assigning some items with known weights to bins with consistent capacities is necessary for the bin packing problem. The goal is to use the smallest possible boxes while ensuring everything is put...

6 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.