# Integer and Long Wrapper Classes in Java

|We have already explored the abstract class

Numberin the previous tutorial, which defines the super-class implemented by classes that wrap or encapsulate the numeric types likebyte,short,int,longfloat, anddouble. In this tutorial, we shall work with the larger non-floating point numeric type wrappers,i.e.andInteger.Long

### Integer Wrapper Class

The **Integer** wrapper class is used to wrap the values of the *int* type. The **Integer** constructor takes an *int* number as an argument or a *String* that contains a whole numeric value. The constructor for the **Integer** class is shown here:

Integer(int num) Integer(String str)throws NumberFormatException

The construction of an **Integer** type object is shown below:

Integer i = new Integer(100);

**Read More** – Character, Boolean , Byte. Short Wrapper classes

#### Some of the methods defined by the Integer Wrapper Class

METHODS | DESCRIPTION |
---|---|

static int bitCount(int num) | Returns the number of set bits in num. |

byte byteValue() | Returns the value of invoking object as byte. |

static int compare(int num1, int num2) | Compares the value of num1 and num2. Returns zero if equal. Returns a negative value if num1 is less than num2 and returns a positive value otherwise. |

int compareTo(Integer i) | Compares the numerical value of the invoking object with the object i. Returns zero if equal. Returns a negative value if invoking object’s value is less than i and returns a positive value otherwise. |

static Integer decode(String str)throws NumberFormatException | Returns an Integer object which contains the value stored in str. |

static int divideUnsigned(int dividend, int divisor) | Returns the result of the division as an unsigned value. Added by JDK8. |

boolean equals(Object IntObj) | Returns true if invoking object is equivalent to IntObj, otherwise false. |

static Integer getInteger(Static propName, int default) | Returns the value associated with the environmental property specified by propName. In the case of failure, default is returned. |

static int hashCode(int num) | Returns hash code for num. |

static int lowestOneBit(int num) | Determines the position of the lowest order set bit in num. It returns a value in which only this bit is set. If no bit is set then it returns zero. |

static int max(int num1, int num2) | Returns the maximum of the two. Added by JDK8. |

static int min(int num1, int num2) | Returns the minimum of the two. Added by JDK8. |

static int NumberOfLeadingZeros(int num) | Returns the number of high order zero bits that precede the first high order set bit in num. If num is zero then 32 is returned. |

static int NumberOfTrailingZeros(int num) | Returns the number of low order zero bits that precede the first low order set bit in num. If num is zero then 32 is returned. |

static int parseInt(String str, int radix)throws NumberFormatException | Returns the integer equivalent of the number in str using the specified radix. If the function is written without int radix then the radix used is 10. |

static int remainderUnsinged(int dividend, int radix) | Returns the remainder as an unsigned value. Added by JDK8. |

static int reverse(int num) | Reverses the order of the bits in num and returns the value. |

static int rotateLeft(int num, int n) | Returns the result of rotating num left by n positions. |

static int rotateLeft(int num, int n) | Returns the result of rotating num right by n positions. |

static int sum(int num1, int num2) | Returns the sum of the two numbers. Added by JDK8. |

static String toBinaryString(int num) | Returns a String that contains the binary equivalent of num. |

static String toHexString(int num) | Returns a String that contains the hexadecimal equivalent of num. |

static String toOctalString(int num) | Returns a String that contains the octal equivalent of num. |

static Integer valueOf(int num) | Returns an Integer object that contains the value of num. |

static String toUnsignedString(int num, int radix) | Returns a String that contains the value of num as unsigned int in the radix specified. |

static Integer valueOf(String str, int radix)throws NumberFormatException | Returns an Integer object that contains the value specified by str using the given radix. |

### Long Wrapper Class

The **Long** wrapper class is used to wrap the values of the *long* type. The **Long** constructor takes a *long* number as an argument or a *String* that contains a whole numeric value. The constructor for the **Long** class is shown here:

Long(long num) Long(String str)throws NumberFormatException

The construction of a **Long** type object is shown below:

Long i = new Long(1000);

#### Some of the methods defined by the Integer Wrapper Class

METHODS | DESCRIPTION |
---|---|

static int bitCount(long num) | Returns the number of set bits in num. |

byte byteValue() | Returns the value of invoking object as byte. |

static int compare(long num1, long num2) | Compares the value of num1 and num2. Returns zero if equal. Returns a negative value if num1 is less than num2 and returns a positive value otherwise. |

int compareTo(Long l) | Compares the numerical value of the invoking object with the object l. Returns zero if equal. Returns a negative value if invoking object’s value is less than l and returns a positive value otherwise. |

static Long decode(String str)throws NumberFormatException | Returns a Long object which contains the value stored in str. |

static int divideUnsigned(int dividend, int divisor) | Returns the result of the division as an unsigned value. Added by JDK8. |

boolean equals(Object LongObj) | Returns true if invoking object is equivalent to LongObj, otherwise false. |

static Long getLong(Static propName, long default) | Returns the value associated with the environmental property specified by propName.In the case of failure, default is returned. |

static int hashCode(long num) | Returns hash code for num. |

static long lowestOneBit(long num) | Determines the position of the lowest order set bit in num. It returns a value in which only this bit is set. If no bit is set then it returns zero. |

static long max(long num1, long num2) | Returns the maximum of the two. Added by JDK8. |

static long min(long num1, long num2) | Returns the minimum of the two. Added by JDK8. |

static int NumberOfLeadingZeros(long num) | Returns the number of high order zero bits that precede the first high order set bit in num. If num is zero then 64 is returned. |

static int NumberOfTrailingZeros(long num) | Returns the number of low order zero bits that precede the first low order set bit in num. If num is zero then 64 is returned. |

static long parseLong(String str, int radix)throws NumberFormatException | Returns the long equivalent of the number in str using the specified radix. If the function is written without int radix then the radix used is 10. |

static long remainderUnsinged(long dividend, long radix) | Returns the remainder as an unsigned value. Added by JDK8. |

static long reverse(long num) | Reverses the order of the bits in num and returns the value. |

static long rotateLeft(long num, int n) | Returns the result of rotating num left by n positions. |

static long rotateLeft(long num, int n) | Returns the result of rotating num right by n positions. |

static long sum(long num1, long num2) | Returns the sum of the two numbers. Added by JDK8. |

static String toBinaryString(long num) | Returns a String that contains the binary equivalent of num. |

static String toHexString(long num) | Returns a String that contains the hexadecimal equivalent of num. |

static String toOctalString(long num) | Returns a String that contains the octal equivalent of num. |

static Long valueOf(long num) | Returns a Long object that contains the value of num. |

static String toUnsignedString(long num, int radix) | Returns a String that contains the value of num as unsigned int in the radix specified. |

static Long valueOf(String str, int radix)throws NumberFormatException | Returns a Long object that contains the value specified by str using the given radix. |

### Program to show the use of Integer and Long Wrapper classes

/** * This program shows the use of Integer and Long wrapper classes and some of its functions */ public class IntLongDemo { public static void main(String args[]) { //Wrapping the int value 100 into the object i1 Integer i1 = new Integer(100); //Wrapping the int value 15 into the object i2 Integer i2 = new Integer(15); //l is assigned the long value 4567 Long l = new Long(4567); //Some of the methods are shown below System.out.println("Sum of " + i1 + " and " + i2 + " is : " + Integer.sum(i1, i2)); System.out.println("The maximum of the two Integer objects is : " + Integer.max(i1, i2)); System.out.println("The octal of the number " + l + " is : " + Long.toOctalString(l)); } }

**Output:-**
Sum of 100 and 15 is : 115
The maximum of the two Integer objects is : 100
The octal of the number 4567 is : 10727

**So that’s all for this tutorial. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback.**** Do come back for more because learning paves way for a better understanding.**

*Keep Coding!! Happy Coding!! 🙂*