Advertisement
Constructor | Description |
---|---|
Long(long i) | Constructor of Long wrapper class takes a primitive long value. |
Long(String str) | Constructor of Long wrapper class also takes a String equivalent of a primitive long value. |
class A
{
public static void main(String... ar)
{
Long b1 = new Long(10); //Boxing primitive long to a Long object.
Long b2 = new Long("20"); //Passing primitive long as a String
}
}
class A
{
public static void main(String... ar)
{
Long b1 = new Long(10); //Boxing primitive long in a wrapper Long object.
Long b2 = new Long("20"); //Passing primitive long as a String to be boxed in a Long object.
//Unboxing a wrapper Long object.
System.out.println(b1);
//Unboxing an Integer object using longValue() method
System.out.println(b2.longValue());
}
}
10
20
Advertisement
Methods | Description | int compareTo(Long b) | - Returns a zero if the invoking Long object contains value same as b. - Returns a positive value if invoking Long object's value is > b. - Returns a negative value if invoking Long object's value is < b. |
---|---|
boolean equals (Object ob) | Returns a true if invoking Long object has same value as referred by ob, else false. |
static long parseLong(String s) | Returns a primitive long value if String, s could be converted to a valid long value. |
static Long valueOf(long b) | Returns a Long object after converting it from primitive long value, b. |
static Long valueOf(String s) | Returns a Long object after converting it from a String, s. |
short shortValue() | Returns an primitive short value after converting it from an invoked Long object. |
Long byteValue() | Returns a primitive byte value after converting it from an invoked Long object. |
int intValue() | Returns a primitive int value after converting it from an invoked Long object. |
long longValue() | Returns a primitive long value after converting it from an invoked Long object. |
float floatValue() | Returns aprimitivefloat value after converting it from an invoked Long object. |
double doubleValue() | Returns an primitive double value after converting it from an invoked Long object. |
class A
{
public static void main(String... ar)
{
Long l1 = new Long("10"); //Constructor accepting String value
Long l2 = new Long(10); //Constructor accepting primitive int value
System.out.println("Value in l1 = "+ l1);
System.out.println("Value in l2 = "+ l2);
System.out.println("Invoking l1 to compare with l2 : "+ l1.compareTo(l2));
Long l3 = new Long("15");
Long l4 = new Long(20);
System.out.println("Value in l3 = "+l3);
System.out.println("Value in l4 = "+l4);
System.out.println("Invoking l3 to compare with l4 : "+ l3.compareTo(l4));
System.out.println("Invoking l4 to compare with l3 : "+ l4.compareTo(l3));
}
}
Value in l1 = 10
Value in l2 = 10
Invoking l1 to compare with l2 : 0
Value in l3 = 15
Value in l4 = 20
Invoking l3 to compare with l4 : -1
Invoking l4 to compare with l3 : 1
//Converting String to primitive long value.
import java.util.*;
class A
{
public static void main(String... ar)
{
long l1 = Long.parseLong("200");
long l2 = Long.parseLong("-200");
System.out.println("Primitive long value in l1 : "+ l1);
System.out.println("Primitive long value in l2 : "+ l2);
}
}
Primitive long value in l1 : 200
Primitive long value in l2 : -200
class A
{
public static void main(String... ar)
{
//long l= 100L; works but,
Long b1 = new Long("100L"); //Passing a String value that cannot be converted to a int value.
}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "100L"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.(Unknown Source)
at A.main(Long4.java:8)
class A
{
public static void main(String... ar)
{
int i = Integer.parseInt("-100l"); //Passing a String value that can't be converted to an int.
}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "-100L"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.(Unknown Source)
at A.main(Long4.java:7)
import java.util.*;
class A
{
public static void main(String... ar)
{
Long l = new Long(10000); //Passing a primitive long value to constructor of wrapper class, Long.
System.out.println("Value in wrapped Long object, l : "+ l);
byte b = l.byteValue(); //Returns a primitive byte value out of a wrapped Long object
short s= l.shortValue(); //Returns a primitive short value out of a wrapped Long object
int i = l.intValue(); //Returns a primitive int value out of a wrapped Long object
long l = l.longValue(); //Returns a primitive long value out of a wrapped Long object
float f = l.floatValue(); //Returns a primitive float value out of a wrapped Long object
double d = l.doubleValue(); //Returns a primitive double value out of a wrapped Long object
System.out.println("byte value of Long object, l : " + b);
System.out.println("short value of Long object, l : " + s);
System.out.println("int value of Long object, l : " + i);
System.out.println("long value of Long object, l : " + l);
System.out.println("float value of Long object, l : " + f);
System.out.println("double value of Long object, l : "+ d);
}
}
Value in wrapped Long object, l : 10000
byte value of Long object, l : 10000
short value of Long object, l : 10000
int value of Long object, l : 10000
long value of Long object, l : 10000
float value of Long object, l : 10000.0
double value of Long object, l : 10000.0
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement