http://www.hudatutorials.com
Integer Class
The Integer class wraps the value of primitive data type int into Integer object. An object of type Integer contains a single field whose type is int. In other words the wrapper classes create objects for primitive data types. The wrapper classes are Boolean, Byte, Character, Short, Integer, Float, Long and Double.
This class has following constructors.
Integer(int value)
Constructs a newly allocated Integer object that represents the specified int value.
Integer(String s)
Constructs a newly allocated Integer object that represents the int value indicated by the String parameter.
/* Integer Class Example */
/* Save with file name IntegerExample.java */
public class IntegerExample
{
public static void main(String args[])
{
//INTEGER DECLARATION
Integer b;
//MEMORY ALLOCATION FOR INTEGER
b = new Integer(101);
//INTEGER CLASS OUTPUT
System.out.println();
System.out.println("Integer Class Example");
System.out.println("======");
System.out.println();
//RETURNS byte PRIMITIVE DATA TYPE
System.out.println("byte Value is : "+ b.byteValue());
//RETURNS short PRIMITIVE DATA TYPE
System.out.println("short Value is : "+ b.shortValue());
//RETURNS double PRIMITIVE DATA TYPE
System.out.println("double Value is : "+ b.doubleValue());
//RETURNS float PRIMITIVE DATA TYPE
System.out.println("float Value is : "+ b.floatValue());
//RETURNS int PRIMITIVE DATA TYPE
System.out.println("int Value is : "+ b.intValue());
//RETURNS long PRIMITIVE DATA TYPE
System.out.println("long Value is : "+ b.longValue());
}
}
/* Integer Class Example 2 */
/* Save with file name IntegerExample2.java */
public class IntegerExample2
{
public static void main(String args[])
{
//INTEGER DECLARATION
Integer b, b2;
//MEMORY ALLOCATION FOR INTEGER
b = new Integer(101);
b2 = new Integer("102");
//INTEGER CLASS OUTPUT
System.out.println();
System.out.println("Integer Class Example");
System.out.println("======");
System.out.println();
System.out.println("b Value is : "+ b.intValue());
System.out.println("b2 Value is : "+ b2.intValue());
System.out.println();
System.out.println("b compareTo b2 : "+ b.compareTo(b2));
System.out.println("b equals b2 : "+ b.equals(b2));
}
}
In the following example you can learn how to use static methods of Integer Class. If you use static methods you need not to create the class instance. You can call static methods using the class name as reference.
/* Integer Class Example 3 */
/* Save with file name IntegerExample3.java */
public class IntegerExample3
{
public static void main(String args[])
{
//INTEGER CLASS STATIC METHODS USAGE
System.out.println("Integer Class Example");
System.out.println("======");
System.out.println();
//RETURN Integer OBJECT WITH THE SPECIFIED PRIMITIVE int DATA TYPE
System.out.println("valueOf : "+ Integer.valueOf(101));
//RETURN Integer OBJECT WITH THE SPECIFIED
//PRIMITIVE int DATA TYPE AS String
System.out.println("valueOf : "+ Integer.valueOf("103"));
//RETURN PRIMITIVE int DATA TYPE WITH THE SPECIFIED
//PRIMITIVE int DATA TYPE AS String
System.out.println("parseInt : "+ Integer.parseInt("105"));
}
}
1