Advertisement



< Prev
Next >



Long Wrapper Class





Long wrapper class is used to create an object version of a primitive long value.





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());

}
}


Output-


10
20



Advertisement




Some important methods of Long wrapper class


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.





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


Method compareTo(Long l) takes Long class object and it -
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));

}
}


Output-


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





Convering a String to a primitive long value, using parseLong() method.

Method parseLong(String s), takes a string and parse it to a primitive long value.

//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);
}
}


Output-


Primitive long value in l1 : 200
Primitive long value in l2 : -200





Exception while converting a String to an Long object or a primitive long value


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

C#, PYTHON and DJANGO


Advertisement