Class Integer in Java
Class Integer wraps a value of the primitive type int into an immutable Object. An Object of type Integer contains a single field whose type is int. This is useful when we need to treat an int value as an object.
This class also contains useful minimum and maximum constants and useful conversion methods. parseInt() and valueOf() convert a string to an int or to an Integer, respectively. Each can take a radix argument to specify the base that the value is represented in. decode() also converts a String to an Integer. It assumes a hexadecimal number if the string begins with “0X” or “0x,” or an octal number if the string begins with “0”. Otherwise, a decimal number is assumed.
toString() converts in the other direction, and the static version takes a radix argument. toBinaryString(), toOctalString(), and toHexString() convert an int to a string using base 2, base 8, and base 16. These methods treat the integer as an unsigned value.
Other routines return the value of an Integer as various primitive types, and finally, the getInteger() methods return the integer value of a named property from the system property list or the specified default value.
The Integer class also provides a number of utility methods for converting int values to other primitive types and for converting int values to strings and vice versa.
The structure of the class Integer is given below:
public final class java.lang.Integer extends java.lang.Number{
//Member Elements
public final static int MAX_VALUE;//MAX_VALUE=2147483647
public final static int MIN_VALUE;//MIN_VALUE=-2147483648
public static final Class TYPE;
//Constructors
public Integer(int value);//constructs a newly created Integer object and allocate memory space that represents the primitive int argument
public Integer(String str)throws NumberFormatException;//Constructs a newly allocated Integer object that represents the int value of type int represented by String str.The string is converted to a int value by valueOf() method.This method may throw NumberFormatException if the string does not contain a parsable number.
//Methods:
public double doubleValue();
public boolean equals(Object obj);
public float floatValue();
public string toString();
public static Integer decode(String nm) throws NumberFormatException;
public static Integer getInteger(String value);
public static Integer getInteger(String value,int value2);
public static Integer getInteger(String value,Integer value2);
public static int parseInt(String str)throws NumberFormatException;
public static int parseInt(String str,int radix)throws NumberFormatException;
public static String toBinaryString(int i);
public static String toOctalString(int i);
public static String toHexString(int i);
public static String toString(int i);
public static String toString(int i,int radix);
public static Integer valueOf(String str)throws NumberFormatException;
public static Integer valueOf(String str,int radix)throws NumberFormatException;
public int hashCode();
public int intValue();
public long longValue();
public byte byteValue();
public short shortValue();
}
The details of the class structure are given as follows:
public static final int MAX_VALUE;
public static final int MAX_VALUE is the largest value that can be represented by an int.
public static final int MIN_VALUE;
public static final int MIN_VALUE is the smallest value that can be represented by an int.
public final static Class TYPE;
public final static Class TYPE represents the type int. It is always true that Integer.TYPE == int.class.
public Integer(int value);
public Integer(int value) constructor creates an Integer object with the specified int value.
Parameter
value -The int value to be encapsulated by this object.
public Integer(String s);
public Integer(String s) constructor constructs an Integer object with the value specified by the given string. The string should consist of one or more digit characters. The digit characters can be preceded by a single `-‘ character. If the string contains any other characters, the constructor throws a NumberFormatException.
Parameter
s – The string to be made into an Integer object.
public static Integer decode(String nm)
public static Integer decode(String nm) method returns an Integer object that encapsulates the given value.
This method returns an Integer object that encapsulates the given value.
Parameter
nm – A String representation of the value to be encapsulated by an Integer object. If the string begins with # or 0x, it is a radix 16 representation of the value. If the string begins with 0, it is a radix 8 representation of the value. Otherwise, it is assumed to be a radix 10 representation of the value.
public static Integer getInteger(String nm);
public static Integer getInteger(String nm) method retrieves the value of the named system property and returns it as an Integer object. The method obtains the value of the system property as a String using System.getProperty().
If the value of the property begins with 0x or # and is not followed by a minus sign, the rest of the value is parsed as a hexadecimal integer. If the value begins with 0, it’s parsed as an octal integer; otherwise, it’s parsed as a decimal integer.
This method returns the value of the system property as an Integer object, or an Integer object with the value 0 if the named property does not exist or cannot be parsed.
Parameter
nm – The name of a system property.
public static Integer getInteger(String nm, int val);
public static Integer getInteger(String nm, int val) method retrieves the value of the named system property and returns it as an Integer object. The method obtains the value of the system property as a String using System.getProperty().
If the value of the property begins with 0x or # and is not followed by a minus sign, the rest of the value is parsed as a hexadecimal integer. If the value begins with 0, it’s parsed as an octal integer; otherwise, it’s parsed as a decimal integer.
This method returns the value of the system property as an Integer object, or an Integer object with the value val if the named property does not exist or cannot be parsed.
Parameter
nm-The name of a system property.
val -A default int value for the property.
public static Integer getInteger(String nm, Integer val);
public static Integer getInteger(String nm, Integer val) method retrieves the value of the named system property and returns it as an Integer object. The method obtains the value of the system property as a String using System.getProperty().
If the value of the property begins with 0x or # and is not followed by a minus sign, the rest of the value is parsed as a hexadecimal integer. If the value begins with 0, it’s parsed as an octal integer; otherwise, it’s as a decimal integer.
This method returns the value of the system property as an Integer object, or the Integer object val if the named property does not exist or cannot be parsed.
Parameter
nm -The name of a system property.
val – A default Integer value for the property.
public static int parseInt(String s);
public static int parseInt(String s) method returns the numeric value of the integer represented by the contents of the given String object.
The String must contain only decimal digits, except that the first character may be a minus sign.
This method returns the numeric value of the integer represented by the String object.
Parameter
s -The String to be converted to an int value.
public static int parseInt(String s, int radix);
public static int parseInt(String s, int radix) method returns the numeric value of the integer represented by the contents of the given String object in the specified radix. The String must contain only valid digits of the specified radix, except that the first character may be a minus sign. The digits are parsed in the specified radix to produce the numeric value.
This method returns the numeric value of the integer represented by the String object in the specified radix.
Parameter
s – The String to be converted to an int value.
radix -The radix used in interpreting the characters in the String as digits. This value must be in the range of Character.MIN_RADIX to Character.MAX_RADIX. If radix is in the range 2 through 10, only characters for which the Character.isDigit() method returns true are considered to be valid digits. If radix is in the range 11 through 36, characters in the ranges ‘A’ through ‘Z’ and ‘a’ through ‘z’ may be considered valid digits.
public static String toBinaryString(int i);
public static String toBinaryString(int i) method returns a String object that contains the representation of the given value as an unsigned binary number. To convert the given value to an unsigned quantity, the method simply uses the value as if it were not negative.
In other words, if the given value is negative, the method adds 2^32 to it. Otherwise, the value is used as it is. The string returned by this method contains a sequence of one or more `0′ and `1′ characters. The method returns “0” if its argument is 0. Otherwise, the string returned by this method begins with ‘1’.
This method returns a string that contains the binary representation of the given value.
Parameter
i -The int value to be converted to a string.
public static String toHexString(int i);
public static String toHexString(int i) method returns a String object that contains the representation of the given value as an unsigned hexadecimal number. To convert the given value to an unsigned quantity, the method simply uses the value as if it were not negative. In other words, if the given value is negative, the method adds 2^32 to it. Otherwise, the
value is used as it is.
The string returned by this method contains a sequence of one or more of the characters ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’,’6′, ‘7’, ‘8’, ‘9’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, and ‘f’. The method returns “0” if its argument is 0. Otherwise, the string returned by this method does not begin with ‘0’. To produce a string that contains upper- instead of lowercase letters, use the String.toUpperCase() method.
This method returns a string that contains the hexadecimal representation of the given value.
Parameter
i -The int value to be converted to a string.
public static String toOctalString(int i);
public static String toOctalString(int i) method returns a String object that contains a representation of the given value as an unsigned octal number. To convert the given value to an unsigned quantity, the method simply uses the value as if it were not negative. In other words, if the given value is negative, the method adds 2^32 to it. Otherwise, the value is used as it is.
The string returned by this method contains a sequence of one or more of the characters ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’,’6′, and ‘7’. The method returns “0” if its argument is 0. Otherwise, the string returned by this method does not begin with ‘0’.
This method returns a string that contains the octal representation of the given value.
Parameter
i -The int value to be converted to a string.
public static String toString(int i);
public static String toString(int i) method returns a String object that contains the decimal representation of the given value.
This method returns a string that begins with ‘-‘ if the given value is negative. The rest of the string is a sequence of one or more of the characters ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, and ‘9’. This method returns “0” if its argument is 0. Otherwise, the string returned by this method does not begin with “0” or “-0”.
This method returns the string representation of the given value.
Parameter
i -The int value to be converted to a string.
public static String toString(int i, int radix);
public static String toString(int i, int radix) method returns a String object that contains the representation of the given value in the specified radix.
This method returns a string that begins with ‘-‘ if the given value is negative. The rest of the string is a sequence of one or more characters that represent the magnitude of the given value. The characters that can appear in the sequence are determined by the value of radix. If N is the value of radix, the first N characters on the following line can appear in the sequence: 0123456789abcdefghijklmnopqrstuvwxyz
The method does not verify that radix is in the proper range. If radix is less than Character.MIN_RADIX or greater than Character.MAX_RADIX, the value 10 is used instead of the given value. This method returns “0” if its argument is 0. Otherwise, the string returned by this method does not begin with “0” or “-0”.
This method returns the string representation of the given value in the specified radix.
Parameter
i -The int value to be converted to a string.
radix – The radix used in converting the value to a string. This value must be in the range
Character.MIN_RADIX to Character.MAX_RADIX.
public static Integer valueOf(String s);
public static Integer valueOf(String s) method Constructs an Integer object with the value specified by the given string. The string should consist of one or more digit characters. The digit characters can be preceded by a single ‘-‘ character. If the string contains any other characters, the method throws a NumberFormatException.
This method returns the Integer object constructed from the string.
Parameter
s – The string to be made into an Integer object.
public static Integer valueOf(String s, int radix);
public static Integer valueOf(String s, int radix) method Constructs an Integer object with the value specified by the given string in the specified radix. The string should consist of one or more digit characters or characters in the range ‘A’ to ‘Z’ or ‘a’ to ‘z’ that are considered
digits in the given radix. The digit characters can be preceded by a single `-‘ character. If the string contains any other characters, the method throws a NumberFormatException.
This method returns the Integer object constructed from the string.
Parameter
s – The string to be made into an Integer object.
radix – The radix used in converting the string to a value. This value must be in the range
Character.MIN_RADIX to Character.MAX_RADIX.
public byte byteValue();
public byte byteValue() method returns the value of this object as a byte. The high order bits of the value are discarded.
This method returns the value of this object as a byte.
public double doubleValue();
public double doubleValue() method returns the value of this object as a double.
This method returns the value of this object as a double.
public boolean equals(Object obj);
public boolean equals(Object obj) method returns true if obj is an instance of Integer and it contains the same value as the object this method is associated with.
This method returns true if the objects are equal; false if they are not.
Parameter
obj – The object to be compared with this object.
public float floatValue();
public float floatValue() method returns the value of this object as a float. Rounding may occur.
This method returns the value of this object as a float.
public int hashCode();
public int hashCode() method returns a hashcode computed from the value of this object.
This method returns a hashcode based on the int value of the object.
public int intValue();
public int intValue() method returns the value of this object as an int.
This method returns the value of this object as an int.
public long longValue();
public long longValue() method returns the value of this object as a long.
This method returns the value of this object as a long.
public short shortValue();
public short shortValue() method returns the value of this object as a short. The high order bits of the value are discarded.
This method returns the value of this object as a short.
public String toString();
public String toString() method returns a String object that contains the decimal representation of the value of this object.
This method returns a string that begins with ‘-‘ if the value is negative. The rest of the string is a sequence of one or more of the characters ‘0’, ‘1’, ‘2’,’3′, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, and ‘9’. This method returns “0” if the value of the object is 0. Otherwise, the string returned by this method does not begin with “0” or “-0”.
This method returns the string representation of the value of this object.
Apart from these Compiler class also has inherited methods from class- Object. They are as follows:
- clone()
- finalize()
- hashCode()
- notifyAll()
- wait()
- wait(long, int)
- equals(Object)
- getClass()
- notify()
- toString()
- wait(long)
How Integer.ParseInt Method Works In Java?
Integer.ParseInt is very commonly used method in java.The parameter to this method will be String. So It works on String only.But what kind of String??
Those which can represent ASCII values for digit(0-…9). Remember Integer.ParseInt only works on string representation of numeric value but not something like “five”.
int x=Integer.parseInt("9");
//valid
int y=Integer.parseInt("nine");
//invalid compilation error
Let us check the syntax:
- static int parseInt(String s) // here s is the string representation of Numeric value
- static int parseInt(String s, int radix) //This returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input.
Let us check out one example:
public class parseTest{
public static void main(String args[]){
int x =Integer.parseInt("5");
double c = Double.parseDouble("8");
int b = Integer.parseInt("444",16);
System.out.println(x);
System.out.println(c);
System.out.println(b);
}
Output
5
8.00
1092