The Long class is simply a wrapper class for the primitive type long. It wraps the long primitive value to an object. An object of type Long contains a single field whose type is long. So what would be the benefit if wraps around the primitive data type to an object? Well basically we would be able to access all the methods that is readily available on the Long class which is very useful.In addition, this class provides several methods for converting a long to a String and a String to a long, as well as other constants and methods useful when dealing with a long.

Long Class Syntax

public final class Long
extends Number
implements Comparable<Long>

Long Class Compatibility Version

Long Class is available since JDK 1.0

Long Class Basic Usage

The Long class as part of the java.lang package is one of the classes of the java api not only being one of the oldest class but also one that is being widely used. Let’s tackle the basics of using the Long class. To begin with, let’s tackle first how to instantiate a Long object

Long value = new Long(12L);

Since we are dealing with long, the suffix L or l is required. This is a common mistake for most of the newbie java programmer.

Basically the declaration of a Long object is similar on how we assign a primitive data type. Similarly to convert Long primitive the long object has only two possible values, which is either true or false. This is a straightforward method to instantiate a Byte object type. Let’s take another way to do this.

Long value = 12l;

The 2nd method is using the autoboxing concept of java programming language which directly converts a primitive data type to its corresponding wrapper class.  If you would like to go more into details, I am recommending for you to read the official java documentation regarding autoboxing and unboxing.

There two constructor available to Long class which we can use to instantiate a Character object:

Constructor Description
Long(long value) Constructs a newly allocated Long object that represents the specified long argument.
Long(String s) Constructs a newly allocated Long object that represents the long value indicated by the String parameter.

Long Class Test of Equality

Normally when we do equality test for long primitive data type we do like this:

		long val1= 12L;
		long val2 = 13L;
		if(val1==val2){
			System.out.println("They are equal");
		}
		else{
			System.out.println("Values are not equal");
		}

This will no longer work in dealing with Long object type. To test equality we need to make use of equals which is a method inherited from Object class.

		Long val1 = 12L;
		Longr val2 = new Long(12L);
		if(val1.equals(val2)){
			System.out.println("They are equal");
		}
		else{
			System.out.println("They are not equal");			
		}

Long Class Method Usage Examples

Modifier and Type Method and Description
static int bitCount​(long i)
Returns the number of one-bits in the two’s complement binary representation of the specified long value.
byte byteValue()
Returns the value of this Long as a byte after a narrowing primitive conversion.
static int compare​(long x, long y)
Compares two long values numerically.
int compareTo​(Long anotherLong)
Compares two Long objects numerically.
static int compareUnsigned​(long x, long y)
Compares two long values numerically treating the values as unsigned.
static Long decode​(String nm)
Decodes a String into a Long.
static long divideUnsigned​(long dividend, long divisor)
Returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.
double doubleValue()
Returns the value of this Long as a double after a widening primitive conversion.
boolean equals​(Object obj)
Compares this object to the specified object.
float floatValue()
Returns the value of this Long as a float after a widening primitive conversion.
static Long getLong​(String nm)
Determines the long value of the system property with the specified name.
static Long getLong​(String nm, long val)
Determines the long value of the system property with the specified name.
static Long getLong​(String nm, Long val)
Returns the long value of the system property with the specified name.
int hashCode()
Returns a hash code for this Long.
static int hashCode​(long value)
Returns a hash code for a long value; compatible with Long.hashCode().
static long highestOneBit​(long i)
Returns a long value with at most a single one-bit, in the position of the highest-order (leftmost) one-bit in the specified long value.
int intValue()
Returns the value of this Long as an int after a narrowing primitive conversion.
long longValue()
Returns the value of this Long as a long value.
static long lowestOneBit​(long i)
Returns a long value with at most a single one-bit, in the position of the lowest-order (rightmost) one-bit in the specified long value.
static long max​(long a, long b)
Returns the greater of two long values as if by calling Math.max.
static long min​(long a, long b)
Returns the smaller of two long values as if by calling Math.min.
static int numberOfLeadingZeros​(long i)
Returns the number of zero bits preceding the highest-order (leftmost) one-bit in the two’s complement binary representation of the specified long value.
static int numberOfTrailingZeros​(long i)
Returns the number of zero bits following the lowest-order (rightmost) one-bit in the two’s complement binary representation of the specified long value.
static long parseLong​(CharSequence s, int beginIndex, int endIndex, int radix)
Parses the CharSequence argument as a signed long in the specified radix, beginning at the specified beginIndex and extending to endIndex – 1.
static long parseLong​(String s)
Parses the string argument as a signed decimal long.
static long parseLong​(String s, int radix)
Parses the string argument as a signed long in the radix specified by the second argument.
static long parseUnsignedLong​(CharSequence s, int beginIndex, int endIndex, int radix)
Parses the CharSequence argument as an unsigned long in the specified radix, beginning at the specified beginIndex and extending to endIndex – 1.
static long parseUnsignedLong​(String s)
Parses the string argument as an unsigned decimal long.
static long parseUnsignedLong​(String s, int radix)
Parses the string argument as an unsigned long in the radix specified by the second argument.
static long remainderUnsigned​(long dividend, long divisor)
Returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.
static long reverse​(long i)
Returns the value obtained by reversing the order of the bits in the two’s complement binary representation of the specified long value.
static long reverseBytes​(long i)
Returns the value obtained by reversing the order of the bytes in the two’s complement representation of the specified long value.
static long rotateLeft​(long i, int distance)
Returns the value obtained by rotating the two’s complement binary representation of the specified long value left by the specified number of bits.
static long rotateRight​(long i, int distance)
Returns the value obtained by rotating the two’s complement binary representation of the specified long value right by the specified number of bits.
short shortValue()
Returns the value of this Long as a short after a narrowing primitive conversion.
static int signum​(long i)
Returns the signum function of the specified long value.
static long sum​(long a, long b)
Adds two long values together as per the + operator.
static String toBinaryString​(long i)
Returns a string representation of the long argument as an unsigned integer in base 2.
static String toHexString​(long i)
Returns a string representation of the long argument as an unsigned integer in base 16.
static String toOctalString​(long i)
Returns a string representation of the long argument as an unsigned integer in base 8.
String toString()
Returns a String object representing this Long’s value.
static String toString​(long i)
Returns a String object representing the specified long.
static String toString​(long i, int radix)
Returns a string representation of the first argument in the radix specified by the second argument.
static String toUnsignedString​(long i)
Returns a string representation of the argument as an unsigned decimal value.
static String toUnsignedString​(long i, int radix)
Returns a string representation of the first argument as an unsigned integer value in the radix specified by the second argument.
static Long valueOf​(long l)
Returns a Long instance representing the specified long value.
static Long valueOf​(String s)
Returns a Long object holding the value of the specified String.
static Long valueOf​(String s, int radix)
Returns a Long object holding the value extracted from the specified String when parsed with the radix given by the second argument.