Java.lang.Integer class - Yash-777/LearnJava GitHub Wiki

Java.lang.Integer class in Java geeksforgeeks.org

Integer class is a wrapper class for the primitive type int which contains several methods to effectively deal with a int value like converting it to a string representation, and vice-versa. An object of Integer class can hold a single int value.

  • Constructors:

    1. Integer(int b): Creates a Integer object initialized with the value provided.
      Syntax : public Integer(int b)
      Parameters :
      b : value with which to initialize
      
    2. Integer(String s): Creates a Integer object initialized with the int value provided by string representation.
      Defalut radix is taken to be 10.
      Syntax : public Integer(String s) 
                  throws NumberFormatException
      Parameters :
      s : string representation of the int value 
      Throws :
      NumberFormatException : If the string provided does not represent any int value.
      
  • Methods:

    1. toString() : Returns the string corresponding to the int value.
    2. toHexString() : Returns the string corresponding to the int value in hexadecimal form, that is it returns a string
    3. toOctalString() : Returns the string corresponding to the int value in octal form, that is it returns a string
    4. toBinaryString() : Returns the string corresponding to the int value in binary digits, that is it returns a string representing the int value in hex characters-[0/1]
    5. valueOf() : returns the Integer object initialised with the value provided.
      • valueOf(String val,int radix): Another overloaded function which provides function similar to new
      • valueOf(String val): Another overloaded function which provides function similar to
    6. parseInt() : returns int value by parsing the string in radix provided. Differs from valueOf() as it returns a
    7. getInteger() : returns the Integer object representing the value associated with the given system property or null if it does not exist.
    8. decode() : returns a Integer object holding the decoded value of string provided. String provided must be of the following form else NumberFormatException will be thrown- Decimal- (Sign)Decimal_Number
    9. rotateLeft() : Returns a primitive int by rotating the bits left by given distance in two’s complement form of the value given. When rotating left, the most significant bit is moved to the right hand side, or least significant position i.e. cyclic movement of bits takes place. Negative distance signifies right rotation.
    10. rotateRight() : Returns a primitive int by rotating the bits right by given distance in the twos complement form of the value given. When rotating right, the least significant bit is moved to the left hand side, or most significant position i.e. cyclic movement of bits takes place. Negative distance signifies left rotation.
//Java program to illustrate  
//various Integer methods 
public class Integer_test  
{ 
    public static void main(String args[])  
    { 
        int b = 55; 
        String bb = "45"; 
  
        // Construct two Integer objects 
        Integer x = new Integer(b); 
        Integer y = new Integer(bb); 
  
        // 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)); 
  
        // 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); 
  
        // 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); 
  
        // 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)); 
  
        // 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); 
          
        //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 :

toString(b) = 55
toHexString(b) =37
toOctalString(b) =67
toBinaryString(b) =110111
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
parseInt(bb) = 45
parseInt(bb,6) = 29
getInteger(sun.arch.data.model) = 64
getInteger(abcd) =null
getInteger(abcd,10) =10
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15
rotateLeft(0000 0000 0000 0010 , 2) =8
rotateRight(0000 0000 0000 0010,3) =1073741824

Some more Integer class methods are –

  • byteValue() : returns a byte value corresponding to this Integer Object.
  • shortValue() : returns a short value corresponding to this Integer Object.
  • intValue() : returns a int value corresponding to this Integer Object.
  • longValue() : returns a long value corresponding to this Integer Object.
  • doubleValue() : returns a double value corresponding to this Integer Object.
  • floatValue() : returns a float value corresponding to this Integer Object.
  • hashCode() : returns the hashcode corresponding to this Integer Object.
  • bitcount() : Returns number of set bits in twos complement of the integer given.
⚠️ **GitHub.com Fallback** ⚠️