Advertisement
Constructor | Description |
---|---|
Integer(int i) | Constructor of Integer wrapper class takes a primitive int value. |
Integer(String str) | Constructor of Integer wrapper class also takes a String equivalent of a primitive int value. |
class A
{
public static void main(String... ar)
{
Integer b1 = new Integer(10); //Passing a primitive int value.
Integer b2 = new Integer("20"); //Passing primitive int as a String.
}
}
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());
}
}
int value in an Integer object, i1 : 10
int value in an Integer object, i2 : 20
Advertisement
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. |
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));
}
}
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
//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);
}
}
Primitive int value in b1 : 20
Primitive int value in b2 : -200
//Converting String to object of Integer class by passing it to constructor of Integer class.
class A
{
public static void main(String... ar)
{
Integer b1 = new Integer("100t"); //Passing a String value that cannot be an Integer object.
}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "1
00t"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.(Unknown Source)
at A.main(Byte8.java:7)
//Converting String to primitive int value by passing it to parseInt() method of Integer class.
class A
{
public static void main(String... ar)
{
int i = Integer.parseInt("-27f"); //Passing a String value that can't be converted to an int.
}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "-27f"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at A.main(Int3.java:7)
import java.util.*;
class A
{
public static void main(String... ar)
{
Integer i = new Integer(10); //Converting an int value argumet to wrapper Integer object
System.out.println("Value in wrapped Integer object, i : "+ i);
byte b = i.byteValue(); //Returns a primitive byte value out of a wrapped Integer object
short s= i.shortValue(); //Returns a primitive short value out of a wrapped Integer object
int i = i.intValue(); //Returns a primitive int value out of a wrapped Integer object
long l = i.longValue(); //Returns a primitive long value out of a wrapped Integer object
float f = i.floatValue(); //Returns a primitive float value out of a wrapped Integer object
double d = i.doubleValue(); //Returns a primitive double value out of a wrapped Integer object
System.out.println("byte value of Integer object, i : " + b);
System.out.println("short value of Integer object, i: " + s);
System.out.println("int value of Integer object, i : " + i);
System.out.println("long value of Integer object, i : " + l);
System.out.println("float value of Integer object, i : " + f);
System.out.println("double value of Integer object, i : "+ d);
}
}
Value in wrapped Integer object, i : 10
byte value of Integer object, i : 10
short value of Integer object, i : 10
int value of Integer object, i : 10
long value of Integer object, i : 10
float value of Integer object, i : 10.0
double value of Integer object, i : 10.0
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement