Advertisement



< Prev
Next >



Integer Wrapper Class





Integer wrapper class is used to create an object version of a primitive int value.





class A
{
public static void main(String... ar)
{

Integer i1 = new Integer(10); 		//Boxing primitive int in an Integer object.
Integer i2 = new Integer("20"); 	//Passing a primitive int as a String 


//Unboxing an Integer object.
System.out.println("int value in an Integer object, i1 : "+ i1);	


//Unboxing an Integer object using intValue() method
System.out.println("int value in an Integer object, i2 : "+ i2.intValue());
}
}

Output-

int value in an Integer object, i1 : 10
int value in an Integer object, i2 : 20



Advertisement




Some important methods of Integer wrapper class


Methods Description
int compareTo(Integer b) Returns a zero if the invoked Integer object contains the same value as b. Returns a positive value if the invoked Integer object contains greater value than b. Returns a negative value if the invoked Integer object contains smaller value than b.
boolean equals(Object ob) Returns a true if invoked Long object has same value as referred by ob, else false.
static int parseInteger(String s) Returns a primitive int value if String, s could be converted to a valid int value.
static Integer valueOf(int b) Returns an Integer object after converting it from a primitive int value, b.
static Integer valueOf(String s) Returns a Integer object after converting it from a String, s.
short shortValue() Converts a Integer object to a primitive short value and returns it.
byte byteValue() Converts an Integer object to a primitive byte value and returns it.
int intValue() Converts an Integer object to a primitive int value and returns it
long longValue() Converts an Integer object to a primitive long value and returns it
float floatValue() Converts an Integer object to a primitive float value and returns it
double doubleValue() Converts an Integer object to a primitive double value and returns it.





Using compareTo() method to compare values in two Integer objects.


Method compareTo(Integer i) takes Integer class type object and it -
class A
{
public static void main(String... ar)
{ 
Integer i1 = new Integer("10"); //Constructor accepting String value
Integer i2 = new Integer(10);	  //Constructor accepting primitive int value

System.out.println("Value in i1 = "+ i1);
System.out.println("Value in i2 = "+ i2);

System.out.println("Invoking i1 to compare with i2 : "+ i1.compareTo(i2));

Integer i3 = new Integer("11"); //Passing primitive int as a String to Constructor
Integer i4 = new Integer(20);  //Passing a primitive int directly to Constructor

System.out.println("Value in i3 = "+i3);
System.out.println("Value in i4 = "+i4);

System.out.println("Invoking i3 to compare with i4 : "+ i3.compareTo(i4));

System.out.println("Invoking i4 to compare with i3 : "+ i4.compareTo(i3));

}
}


Output-


Value in i1 = 10
Value in i2 = 10
Invoking i1 to compare with i2 : 0
Value in i3 = 11
Value in i4 = 20
Invoking i3 to compare with i4 : -1
Invoking i4 to compare with i3 : 1





To convert a String to a primitive int value using parseInteger() method.


Method parseInteger(), takes a string value and parse it to primitive int value.
//Converting String to primitive int value.

import java.util.*;

class A
{
public static void main(String... ar)
{
int b1 = Integer.parseInteger("20");
int b2 = Integer.parseInteger("-200");

System.out.println("Primitive int value in b1 : "+ b1);
System.out.println("Primitive int value in b2 : "+ b2);
}
}



Output-


Primitive int value in b1 : 20
Primitive int value in b2 : -200





Exception while converting a String to an Integer object


Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement