Package java.lang

Class Integer

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_VALUE
      Constant for the maximum int value, 231-1.
      static int MIN_VALUE
      Constant for the minimum int value, -231.
      static int SIZE
      Constant for the number of bits needed to represent an int in two's complement form.
    • Constructor Summary

      Constructors 
      Constructor Description
      Integer​(int value)
      Constructs a new Integer with the specified primitive integer value.
      Integer​(String string)
      Constructs a new Integer from the specified string.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static int bitCount​(int i)
      Counts the number of 1 bits in the specified integer; this is also referred to as population count.
      byte byteValue()
      Returns this object's value as a byte.
      static int compare​(int lhs, int rhs)
      Compares two int values.
      int compareTo​(Integer object)
      Compares this object to the specified integer object to determine their relative order.
      static Integer decode​(String string)
      Parses the specified string and returns a Integer instance if the string can be decoded into an integer value.
      double doubleValue()
      Returns this object's value as a double.
      boolean equals​(Object o)
      Compares this instance with the specified object and indicates if they are equal.
      float floatValue()
      Returns this object's value as a float.
      int hashCode()
      Returns an integer hash code for this object.
      static int highestOneBit​(int i)
      Determines the highest (leftmost) bit of the specified integer that is 1 and returns the bit mask value for that bit.
      int intValue()
      Gets the primitive value of this int.
      long longValue()
      Returns this object's value as a long.
      static int lowestOneBit​(int i)
      Determines the lowest (rightmost) bit of the specified integer that is 1 and returns the bit mask value for that bit.
      static int numberOfLeadingZeros​(int i)
      Determines the number of leading zeros in the specified integer prior to the highest one bit.
      static int numberOfTrailingZeros​(int i)
      Determines the number of trailing zeros in the specified integer after the lowest one bit.
      static int parseInt​(String string)
      Parses the specified string as a signed decimal integer value.
      static int parseInt​(String string, int radix)
      Parses the specified string as a signed integer value using the specified radix.
      static int reverse​(int i)
      Reverses the order of the bits of the specified integer.
      static int reverseBytes​(int i)
      Reverses the order of the bytes of the specified integer.
      static int rotateLeft​(int i, int distance)
      Rotates the bits of the specified integer to the left by the specified number of bits.
      static int rotateRight​(int i, int distance)
      Rotates the bits of the specified integer to the right by the specified number of bits.
      short shortValue()
      Returns this object's value as a short.
      static int signum​(int i)
      Returns the value of the signum function for the specified integer.
      static String toBinaryString​(int i)
      Converts the specified integer into its binary string representation.
      static int toCharArray​(char[] string, int off, int val)
      Converts integer to a char array.
      static String toHexString​(int i)
      Converts the specified integer into its hexadecimal string representation.
      String toString()
      Returns a string containing a concise, human-readable description of this object.
      static String toString​(int i)
      Converts the specified integer into its decimal string representation.
      static Integer valueOf​(int i)
      Returns a Integer instance for the specified integer value.
      static Integer valueOf​(String string)
      Parses the specified string as a signed decimal integer value.
      static Integer valueOf​(String string, int radix)
      Parses the specified string as a signed integer value using the specified radix.
    • Field Detail

      • MAX_VALUE

        public static final int MAX_VALUE
        Constant for the maximum int value, 231-1.
        See Also:
        Constant Field Values
      • MIN_VALUE

        public static final int MIN_VALUE
        Constant for the minimum int value, -231.
        See Also:
        Constant Field Values
      • SIZE

        public static final int SIZE
        Constant for the number of bits needed to represent an int in two's complement form.
        Since:
        1.5
        See Also:
        Constant Field Values
    • Constructor Detail

      • Integer

        public Integer​(int value)
        Constructs a new Integer with the specified primitive integer value.
        Parameters:
        value - the primitive integer value to store in the new instance.
    • Method Detail

      • byteValue

        public byte byteValue()
        Description copied from class: Number
        Returns this object's value as a byte. Might involve rounding and/or truncating the value, so it fits into a byte.
        Overrides:
        byteValue in class Number
        Returns:
        the primitive byte value of this object.
      • compareTo

        public int compareTo​(Integer object)
        Compares this object to the specified integer object to determine their relative order.
        Specified by:
        compareTo in interface Comparable<Integer>
        Parameters:
        object - the integer object to compare this object to.
        Returns:
        a negative value if the value of this integer is less than the value of object; 0 if the value of this integer and the value of object are equal; a positive value if the value of this integer is greater than the value of object.
        Since:
        1.2
        See Also:
        Comparable
      • compare

        public static int compare​(int lhs,
                                  int rhs)
        Compares two int values.
        Parameters:
        lhs - First value.
        rhs - Second value.
        Returns:
        0 if lhs = rhs, less than 0 if lhs < rhs, and greater than 0 if lhs > rhs.
        Since:
        1.7
      • decode

        public static Integer decode​(String string)
                              throws NumberFormatException
        Parses the specified string and returns a Integer instance if the string can be decoded into an integer value. The string may be an optional minus sign "-" followed by a hexadecimal ("0x..." or "#..."), octal ("0..."), or decimal ("...") representation of an integer.
        Parameters:
        string - a string representation of an integer value.
        Returns:
        an Integer containing the value represented by string.
        Throws:
        NumberFormatException - if string cannot be parsed as an integer value.
      • doubleValue

        public double doubleValue()
        Description copied from class: Number
        Returns this object's value as a double. Might involve rounding.
        Specified by:
        doubleValue in class Number
        Returns:
        the primitive double value of this object.
      • equals

        public boolean equals​(Object o)
        Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must be an instance of Integer and have the same integer value as this object.
        Overrides:
        equals in class Object
        Parameters:
        o - the object to compare this integer with.
        Returns:
        true if the specified object is equal to this Integer; false otherwise.
        See Also:
        Object.hashCode()
      • floatValue

        public float floatValue()
        Description copied from class: Number
        Returns this object's value as a float. Might involve rounding.
        Specified by:
        floatValue in class Number
        Returns:
        the primitive float value of this object.
      • hashCode

        public int hashCode()
        Description copied from class: Object
        Returns an integer hash code for this object. By contract, any two objects for which Object.equals(java.lang.Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

        Note that hash values must not change over time unless information used in equals comparisons also changes.

        See Writing a correct hashCode method if you intend implementing your own hashCode method.

        Overrides:
        hashCode in class Object
        Returns:
        this object's hash code.
        See Also:
        Object.equals(java.lang.Object)
      • intValue

        public int intValue()
        Gets the primitive value of this int.
        Specified by:
        intValue in class Number
        Returns:
        this object's primitive value.
      • longValue

        public long longValue()
        Description copied from class: Number
        Returns this object's value as a long. Might involve rounding and/or truncating the value, so it fits into a long.
        Specified by:
        longValue in class Number
        Returns:
        the primitive long value of this object.
      • parseInt

        public static int parseInt​(String string)
                            throws NumberFormatException
        Parses the specified string as a signed decimal integer value. The ASCII character - ('-') is recognized as the minus sign.
        Parameters:
        string - the string representation of an integer value.
        Returns:
        the primitive integer value represented by string.
        Throws:
        NumberFormatException - if string cannot be parsed as an integer value.
      • parseInt

        public static int parseInt​(String string,
                                   int radix)
                            throws NumberFormatException
        Parses the specified string as a signed integer value using the specified radix. The ASCII character - ('-') is recognized as the minus sign.
        Parameters:
        string - the string representation of an integer value.
        radix - the radix to use when parsing.
        Returns:
        the primitive integer value represented by string using radix.
        Throws:
        NumberFormatException - if string cannot be parsed as an integer value, or radix < Character.MIN_RADIX || radix > Character.MAX_RADIX.
      • shortValue

        public short shortValue()
        Description copied from class: Number
        Returns this object's value as a short. Might involve rounding and/or truncating the value, so it fits into a short.
        Overrides:
        shortValue in class Number
        Returns:
        the primitive short value of this object.
      • toString

        public String toString()
        Description copied from class: Object
        Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data.
        Overrides:
        toString in class Object
        Returns:
        a printable representation of this object.
      • toString

        public static String toString​(int i)
        Converts the specified integer into its decimal string representation. The returned string is a concatenation of a minus sign if the number is negative and characters from '0' to '9'.
        Parameters:
        i - the integer to convert.
        Returns:
        the decimal string representation of i.
      • toCharArray

        public static int toCharArray​(char[] string,
                                      int off,
                                      int val)
        Converts integer to a char array. No new string object is created!
        Parameters:
        string - the char array who represents the string.
        off - the start position of the char array.
        val - the integer to convert.
        Returns:
        the length of the string
      • toHexString

        public static String toHexString​(int i)
        Converts the specified integer into its hexadecimal string representation. The returned string is a concatenation of characters from '0' to '9' and 'a' to 'f'.
        Parameters:
        i - the integer to convert.
        Returns:
        the hexadecimal string representation of i.
      • toBinaryString

        public static String toBinaryString​(int i)
        Converts the specified integer into its binary string representation. The returned string is a concatenation of '0' and '1' characters.
        Parameters:
        i - the integer to convert.
        Returns:
        the binary string representation of i.
      • valueOf

        public static Integer valueOf​(String string)
                               throws NumberFormatException
        Parses the specified string as a signed decimal integer value.
        Parameters:
        string - the string representation of an integer value.
        Returns:
        an Integer instance containing the integer value represented by string.
        Throws:
        NumberFormatException - if string cannot be parsed as an integer value.
        See Also:
        parseInt(String)
      • valueOf

        public static Integer valueOf​(String string,
                                      int radix)
                               throws NumberFormatException
        Parses the specified string as a signed integer value using the specified radix.
        Parameters:
        string - the string representation of an integer value.
        radix - the radix to use when parsing.
        Returns:
        an Integer instance containing the integer value represented by string using radix.
        Throws:
        NumberFormatException - if string cannot be parsed as an integer value, or radix < Character.MIN_RADIX || radix > Character.MAX_RADIX.
        See Also:
        parseInt(String, int)
      • highestOneBit

        public static int highestOneBit​(int i)
        Determines the highest (leftmost) bit of the specified integer that is 1 and returns the bit mask value for that bit. This is also referred to as the Most Significant 1 Bit. Returns zero if the specified integer is zero.
        Parameters:
        i - the integer to examine.
        Returns:
        the bit mask indicating the highest 1 bit in i.
        Since:
        1.5
      • lowestOneBit

        public static int lowestOneBit​(int i)
        Determines the lowest (rightmost) bit of the specified integer that is 1 and returns the bit mask value for that bit. This is also referred to as the Least Significant 1 Bit. Returns zero if the specified integer is zero.
        Parameters:
        i - the integer to examine.
        Returns:
        the bit mask indicating the lowest 1 bit in i.
        Since:
        1.5
      • numberOfLeadingZeros

        public static int numberOfLeadingZeros​(int i)
        Determines the number of leading zeros in the specified integer prior to the highest one bit.
        Parameters:
        i - the integer to examine.
        Returns:
        the number of leading zeros in i.
        Since:
        1.5
      • numberOfTrailingZeros

        public static int numberOfTrailingZeros​(int i)
        Determines the number of trailing zeros in the specified integer after the lowest one bit.
        Parameters:
        i - the integer to examine.
        Returns:
        the number of trailing zeros in i.
        Since:
        1.5
      • bitCount

        public static int bitCount​(int i)
        Counts the number of 1 bits in the specified integer; this is also referred to as population count.
        Parameters:
        i - the integer to examine.
        Returns:
        the number of 1 bits in i.
        Since:
        1.5
      • rotateLeft

        public static int rotateLeft​(int i,
                                     int distance)
        Rotates the bits of the specified integer to the left by the specified number of bits.
        Parameters:
        i - the integer value to rotate left.
        distance - the number of bits to rotate.
        Returns:
        the rotated value.
        Since:
        1.5
      • rotateRight

        public static int rotateRight​(int i,
                                      int distance)
        Rotates the bits of the specified integer to the right by the specified number of bits.
        Parameters:
        i - the integer value to rotate right.
        distance - the number of bits to rotate.
        Returns:
        the rotated value.
        Since:
        1.5
      • reverseBytes

        public static int reverseBytes​(int i)
        Reverses the order of the bytes of the specified integer.
        Parameters:
        i - the integer value for which to reverse the byte order.
        Returns:
        the reversed value.
        Since:
        1.5
      • reverse

        public static int reverse​(int i)
        Reverses the order of the bits of the specified integer.
        Parameters:
        i - the integer value for which to reverse the bit order.
        Returns:
        the reversed value.
        Since:
        1.5
      • signum

        public static int signum​(int i)
        Returns the value of the signum function for the specified integer.
        Parameters:
        i - the integer value to check.
        Returns:
        -1 if i is negative, 1 if i is positive, 0 if i is zero.
        Since:
        1.5
      • valueOf

        public static Integer valueOf​(int i)
        Returns a Integer instance for the specified integer value.

        Parameters:
        i - the integer value to store in the instance.
        Returns:
        a Integer instance containing i.
        Since:
        1.5