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. |
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.0Example: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