Integer class is one of the predefined class in Java.
The Integer class wraps a value of the primitive type
The Integer class wraps a value of the primitive type
int
in an object. Like other classes, Integer class also have fields, constructors and methods.First we will see some important fields.
Fields:
Fields are nothing but the variables which are present in the class. All these variables are static.
Fields are nothing but the variables which are present in the class. All these variables are static.
1. MAX_VALUE :
A static field holding the maximum value of the integer can have. i.e 231-1 (or) 2147483647.
2. MIN_VALUE :
A static field holding the maximum value of the integer can have. i.e -231 (or) -2147483648.
"Hereafter if you want to know the maximum and minimum range of integer datatype, don't go for google search. Just use the MAX_VALUE and MIN_VALUE fields in the Integer class".
3.SIZE :
SIZE field is used to indicate the number of bits used to represent an int value in two's complement binary form.
For Example,
class IntegerPractice
{
public static void main(String ... args)
{
Integer n = new Integer(10);
System.out.println(n); // Output : 10
System.out.println(Integer.MAX_VALUE); // Output : 2147483647
System.out.println(Integer.MIN_VALUE); // Output : -2147483648
System.out.println(Integer.SIZE); // Output : 32
}
}
Constructors:
There are two constructor's in Integer class.
1.Integer( int value)
Constructs a newly created Integer object that represents the int value.
2.Integer(String s)
Methods:
2. MIN_VALUE :
A static field holding the maximum value of the integer can have. i.e -231 (or) -2147483648.
"Hereafter if you want to know the maximum and minimum range of integer datatype, don't go for google search. Just use the MAX_VALUE and MIN_VALUE fields in the Integer class".
3.SIZE :
SIZE field is used to indicate the number of bits used to represent an int value in two's complement binary form.
For Example,
class IntegerPractice
{
public static void main(String ... args)
{
Integer n = new Integer(10);
System.out.println(n); // Output : 10
System.out.println(Integer.MAX_VALUE); // Output : 2147483647
System.out.println(Integer.MIN_VALUE); // Output : -2147483648
System.out.println(Integer.SIZE); // Output : 32
}
}
Constructors:
There are two constructor's in Integer class.
1.Integer( int value)
Constructs a newly created Integer object that represents the int value.
2.Integer(String s)
Converts the given string into integer and assign it into the Integer object.
Note: There is no default constructor in Integer class.So, we cannot create an Integer object with no arguments.
Methods:
Now we will see some important methods in Integer class.0
1. public static int parseInt(String s) This method will get the string as input and returns it as integer value.The characters in the string must all be digits.
2. public static int parseInt(String s,int radix)
Parses the string argument as a signed integer in the radix specified by the second argument. The characters in the string must all be digits of the specified radix.
3. public static int reverse(int i)
Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified int value.
4. public int compare(Integer anotherInteger)
This function is used to compare Integer objects.the value
5. String toString()
Returns the given Integer value as a string.
6. byte byteValue()
Returns the Integer value as byte.
7. int intValue()
Returns the Integer value as int.
8. float floatValue()
Returns the Integer value as float.
9. long longValue()
Returns the Integer value as long.
10. short shortValue()
Returns the Integer value as short.
3. public static int reverse(int i)
Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified int value.
4. public int compare(Integer anotherInteger)
This function is used to compare Integer objects.the value
0
if this Integer is equal to the argument Integer.A value less than 0
if this Integer is numerically less than the argument Integer and a value greater than 0 if this Integer is numerically greater than the argument Integer.5. String toString()
Returns the given Integer value as a string.
6. byte byteValue()
Returns the Integer value as byte.
7. int intValue()
Returns the Integer value as int.
8. float floatValue()
Returns the Integer value as float.
9. long longValue()
Returns the Integer value as long.
10. short shortValue()
Returns the Integer value as short.