Integer Wrapper Class in Java

The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.

Integer class Constructors

  • Integer(int value) - Constructs a newly allocated Integer object that represents the specified int value.
  • Integer(String s) - Constructs a newly allocated Integer object that represents the int value indicated by the String parameter.
Example:
int b = 55;
String str= "45";
// Construct two Integer objects
Integer x = new Integer(b);
Integer y = new Integer(str);

Integer class Methods

A diagram shows a list of APIs/Methods that Integer class provides.
In this post, we will learn a few important methods of the Integer wrapper class provides.

toString() methods

  • String toString() - Returns a String object representing this Integer's value.
  • static String toString(int i) - Returns a String object representing the specified integer.
  • static String toString(int i, int radix) - Returns a string representation of the first argument in the radix specified by the second argument.
Example: Example to demonstrate above all toString() methods.
private static void toStringMethods() {
 // toString()
 System.out.println("toString(b) = " + Integer.toString(b));

 // toHexString(),toOctalString(),toBinaryString()
 // converts into hexadecimal, octal and binary forms.
 System.out.println("toHexString(b) =" + Integer.toHexString(b));
 System.out.println("toOctalString(b) =" + Integer.toOctalString(b));
 System.out.println("toBinaryString(b) =" + Integer.toBinaryString(b));

}
Output:
toString(b) = 55
toHexString(b) =37
toOctalString(b) =67
toBinaryString(b) =110111

valueOf() Methods

  • static Integer valueOf(int i) - Returns an Integer instance representing the specified int value.
  • static Integer valueOf(String s) - Returns an Integer object holding the value of the specified String.
  • static Integer valueOf(String s, int radix) - Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.
Example:
private static void valueOfMethods() {
 // valueOf(): return Integer object
 // an overloaded method takes radix as well.
 Integer z = Integer.valueOf(b);
 System.out.println("valueOf(b) = " + z);
 z = Integer.valueOf(bb);
 System.out.println("ValueOf(bb) = " + z);
 z = Integer.valueOf(bb, 6);
 System.out.println("ValueOf(bb,6) = " + z);

}
Output:
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29

parseInt() methods

  • static int parseInt(String s) - Parses the string argument as a signed decimal integer.
  • static int parseInt(String s, int radix) - Parses the string argument as a signed integer in the radix specified by the second argument.
Example:
private static void parseIntMethods() {
 // parseInt(): return primitive int value
 // an overloaded method takes radix as well
 int zz = Integer.parseInt(bb);
 System.out.println("parseInt(bb) = " + zz);
 zz = Integer.parseInt(bb, 6);
 System.out.println("parseInt(bb,6) = " + zz);

}
Output:
parseInt(bb) = 45
parseInt(bb,6) = 29

rotateLeft() and rotateRight() methods

  • static int rotateLeft(int i, int distance) - Returns the value obtained by rotating the two's complement binary representation of the specified int value left by the specified number of bits.
  • static int rotateRight(int i, int distance) - Returns the value obtained by rotating the two's complement binary representation of the specified int value right by the specified number of bits.
Example:
private static void rotateMethods() {
 // rotateLeft and rotateRight can be used
 // to rotate bits by specified distance
 int valrot = 2;
 System.out.println("rotateLeft(0000 0000 0000 0010 , 2) =" + Integer.rotateLeft(valrot, 2));
 System.out.println("rotateRight(0000 0000 0000 0010,3) =" + Integer.rotateRight(valrot, 3));
}
Output:
rotateLeft(0000 0000 0000 0010 , 2) =8
rotateRight(0000 0000 0000 0010,3) =1073741824

decode() method

  • static Integer decode(String nm) - Decodes a String into an Integer.
Example:
private static void decodeMethods() {

 // decode() : decodes the hex,octal and decimal
 // string to corresponding int values.
 String decimal = "45";
 String octal = "005";
 String hex = "0x0f";
 Integer dec = Integer.decode(decimal);
 System.out.println("decode(45) = " + dec);
 dec = Integer.decode(octal);
 System.out.println("decode(005) = " + dec);
 dec = Integer.decode(hex);
 System.out.println("decode(0x0f) = " + dec);

}
Output:
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15

getInteger() methods

  • static Integer getInteger(String nm) - Determines the integer value of the system property with the specified name.
  • static Integer getInteger(String nm, int val) - Determines the integer value of the system property with the specified name.
  • static Integer getInteger(String nm, Integer val) - Returns the integer value of the system property with the specified name.
Example:
private static void getIntegerMethods() {
 // getInteger(): can be used to retrieve
 // int value of system property
 int prop = Integer.getInteger("sun.arch.data.model");
 System.out.println("getInteger(sun.arch.data.model) = " + prop);
 System.out.println("getInteger(abcd) =" + Integer.getInteger("abcd"));

 // an overloaded getInteger() method
 // which return default value if property not found.
 System.out.println("getInteger(abcd,10) =" + Integer.getInteger("abcd", 10));

}
Output:
getInteger(sun.arch.data.model) = 64
getInteger(abcd) =null
getInteger(abcd,10) =10

int,byte,short,long,double,float value() methods

private static void valueMethods() {
 // Construct two Integer objects
 Integer x = new Integer(b);
 // xxxValue can be used to retrieve
 // xxx type value from int value.
 // xxx can be int,byte,short,long,double,float
 System.out.println("bytevalue(x) = " + x.byteValue());
 System.out.println("shortvalue(x) = " + x.shortValue());
 System.out.println("intvalue(x) = " + x.intValue());
 System.out.println("longvalue(x) = " + x.longValue());
 System.out.println("doublevalue(x) = " + x.doubleValue());
 System.out.println("floatvalue(x) = " + x.floatValue());
}
Output:
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.0
floatvalue(x) = 55.0

bitcount()

int value = 45;

// bitcount() : can be used to count set bits
// in twos complement form of the number
System.out.println("Integer.bitcount(value)=" + Integer.bitCount(value));

numberOfTrailingZeros() and numberOfLeadingZeros() methods

// numberOfTrailingZeroes and numberOfLeaadingZeroes
// can be used to count prefix and postfix sequence of 0
System.out.println("Integer.numberOfTrailingZeros(value)=" + Integer.numberOfTrailingZeros(value));
System.out.println("Integer.numberOfLeadingZeros(value)=" + Integer.numberOfLeadingZeros(value));

highestOneBit() method

// highestOneBit returns a value with one on highest
// set bit position
System.out.println("Integer.highestOneBit(value)=" + Integer.highestOneBit(value));

lowestOneBit() method

// highestOneBit returns a value with one on lowest
// set bit position
System.out.println("Integer.lowestOneBit(value)=" + Integer.lowestOneBit(value));

reverse() and reverseytes() methods

// reverse() can be used to reverse order of bits
// reverseytes() can be used to reverse order of bytes
System.out.println("Integer.reverse(value)=" + Integer.reverse(value));
System.out.println("Integer.reverseBytes(value)=" + Integer.reverseBytes(value));

signum() method

// signum() returns -1,0,1 for negative,0 and positive
// values
System.out.println("Integer.signum(value)=" + Integer.signum(value));

hashcode(), equals(), compare() and compareTo() methods

// hashcode() returns hashcode of the object
int hash = x.hashCode();
System.out.println("hashcode(x) = " + hash);

// equals returns boolean value representing equality
boolean eq = x.equals(y);
System.out.println("x.equals(y) = " + eq);

// compare() used for comparing two int values
int e = Integer.compare(x, y);
System.out.println("compare(x,y) = " + e);

// compareTo() used for comparing this value with some
// other value
int f = x.compareTo(y);
System.out.println("x.compareTo(y) = " + f);
Complete Program for Reference
package com.javaguides.corejava.wrapperclasses;

/**
 * 
 * @author javaguides.net
 *
 */
public class IntegerClassExample {

 static int b = 55;
 static String bb = "45";

 private static void toStringMethods() {
  // toString()
  System.out.println("toString(b) = " + Integer.toString(b));

  // toHexString(),toOctalString(),toBinaryString()
  // converts into hexadecimal, octal and binary forms.
  System.out.println("toHexString(b) =" + Integer.toHexString(b));
  System.out.println("toOctalString(b) =" + Integer.toOctalString(b));
  System.out.println("toBinaryString(b) =" + Integer.toBinaryString(b));

 }

 private static void valueOfMethods() {
  // valueOf(): return Integer object
  // an overloaded method takes radix as well.
  Integer z = Integer.valueOf(b);
  System.out.println("valueOf(b) = " + z);
  z = Integer.valueOf(bb);
  System.out.println("ValueOf(bb) = " + z);
  z = Integer.valueOf(bb, 6);
  System.out.println("ValueOf(bb,6) = " + z);

 }

 private static void parseIntMethods() {
  // parseInt(): return primitive int value
  // an overloaded method takes radix as well
  int zz = Integer.parseInt(bb);
  System.out.println("parseInt(bb) = " + zz);
  zz = Integer.parseInt(bb, 6);
  System.out.println("parseInt(bb,6) = " + zz);

 }

 private static void getIntegerMethods() {
  // getInteger(): can be used to retrieve
  // int value of system property
  int prop = Integer.getInteger("sun.arch.data.model");
  System.out.println("getInteger(sun.arch.data.model) = " + prop);
  System.out.println("getInteger(abcd) =" + Integer.getInteger("abcd"));

  // an overloaded getInteger() method
  // which return default value if property not found.
  System.out.println("getInteger(abcd,10) =" + Integer.getInteger("abcd", 10));

 }

 private static void decodeMethods() {

  // decode() : decodes the hex,octal and decimal
  // string to corresponding int values.
  String decimal = "45";
  String octal = "005";
  String hex = "0x0f";
  Integer dec = Integer.decode(decimal);
  System.out.println("decode(45) = " + dec);
  dec = Integer.decode(octal);
  System.out.println("decode(005) = " + dec);
  dec = Integer.decode(hex);
  System.out.println("decode(0x0f) = " + dec);

 }

 private static void rotateMethods() {
  // rotateLeft and rotateRight can be used
  // to rotate bits by specified distance
  int valrot = 2;
  System.out.println("rotateLeft(0000 0000 0000 0010 , 2) =" + Integer.rotateLeft(valrot, 2));
  System.out.println("rotateRight(0000 0000 0000 0010,3) =" + Integer.rotateRight(valrot, 3));
 }

 private static void valueMethods() {
  // Construct two Integer objects
  Integer x = new Integer(b);
  // xxxValue can be used to retrieve
  // xxx type value from int value.
  // xxx can be int,byte,short,long,double,float
  System.out.println("bytevalue(x) = " + x.byteValue());
  System.out.println("shortvalue(x) = " + x.shortValue());
  System.out.println("intvalue(x) = " + x.intValue());
  System.out.println("longvalue(x) = " + x.longValue());
  System.out.println("doublevalue(x) = " + x.doubleValue());
  System.out.println("floatvalue(x) = " + x.floatValue());
 }

 public static void main(String[] args) {

  // Construct two Integer objects
  Integer x = new Integer(b);
  Integer y = new Integer(bb);

  toStringMethods();
  parseIntMethods();
  rotateMethods();
  toStringMethods();
  valueOfMethods();
  decodeMethods();
  getIntegerMethods();
  valueMethods();
  
  
  int value = 45;

  // bitcount() : can be used to count set bits
  // in twos complement form of the number
  System.out.println("Integer.bitcount(value)=" + Integer.bitCount(value));

  // numberOfTrailingZeroes and numberOfLeaadingZeroes
  // can be used to count prefix and postfix sequence of 0
  System.out.println("Integer.numberOfTrailingZeros(value)=" + Integer.numberOfTrailingZeros(value));
  System.out.println("Integer.numberOfLeadingZeros(value)=" + Integer.numberOfLeadingZeros(value));

  // highestOneBit returns a value with one on highest
  // set bit position
  System.out.println("Integer.highestOneBit(value)=" + Integer.highestOneBit(value));

  // highestOneBit returns a value with one on lowest
  // set bit position
  System.out.println("Integer.lowestOneBit(value)=" + Integer.lowestOneBit(value));

  // reverse() can be used to reverse order of bits
  // reverseytes() can be used to reverse order of bytes
  System.out.println("Integer.reverse(value)=" + Integer.reverse(value));
  System.out.println("Integer.reverseBytes(value)=" + Integer.reverseBytes(value));

  // signum() returns -1,0,1 for negative,0 and positive
  // values
  System.out.println("Integer.signum(value)=" + Integer.signum(value));

  // hashcode() returns hashcode of the object
  int hash = x.hashCode();
  System.out.println("hashcode(x) = " + hash);

  // equals returns boolean value representing equality
  boolean eq = x.equals(y);
  System.out.println("x.equals(y) = " + eq);

  // compare() used for comparing two int values
  int e = Integer.compare(x, y);
  System.out.println("compare(x,y) = " + e);

  // compareTo() used for comparing this value with some
  // other value
  int f = x.compareTo(y);
  System.out.println("x.compareTo(y) = " + f);
 }
}
Output:
toString(b) = 55
toHexString(b) =37
toOctalString(b) =67
toBinaryString(b) =110111
parseInt(bb) = 45
parseInt(bb,6) = 29
rotateLeft(0000 0000 0000 0010 , 2) =8
rotateRight(0000 0000 0000 0010,3) =1073741824
toString(b) = 55
toHexString(b) =37
toOctalString(b) =67
toBinaryString(b) =110111
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15
getInteger(sun.arch.data.model) = 64
getInteger(abcd) =null
getInteger(abcd,10) =10
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.0
floatvalue(x) = 55.0
Integer.bitcount(value)=4
Integer.numberOfTrailingZeros(value)=0
Integer.numberOfLeadingZeros(value)=26
Integer.highestOneBit(value)=32
Integer.lowestOneBit(value)=1
Integer.reverse(value)=-1275068416
Integer.reverseBytes(value)=754974720
Integer.signum(value)=1
hashcode(x) = 55
x.equals(y) = false
compare(x,y) = 1
x.compareTo(y) = 1

Reference

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments