Long Wrapper Class in Java

Wrapper classes in Java provide a mechanism to perform the object operations on the java primitive types. One such class is the Long Wrapper Class in Java. It can help us perform the various object operations on the long primitive type.

Long class is a part of the java.lang package.

Declaration of the Long Wrapper class
public final class Long extends Number
        implements Comparable<Long>, Constable, ConstantDesc

Here, Number is the abstract class which declares a bunch of xxxValue() methods like intValue(), longValue(), floatValue(), etc., and the Long class will implement the definition of these methods along with some method of its own.

How to create an object of the Long Wrapper class

There are multiple ways to create objects of the Long wrapper class.

Let’s look at each one by one, and we will also discuss which one should be used to create the objects of the Long class.

Long(long value) constructor
  • Method declaration – public Long(long value)
  • What does it do? It will take a long primitive value as an argument and construct a Long class object representing the specified argument.
public class Codekru {

	public static void main(String[] args) {
		long primitiveValue = 9;
		Long longObject = new Long(primitiveValue);

		System.out.println("value : " + longObject);
	}
}

Output –

value : 9

This is a very easy way to create objects, but this constructor has been marked as deprecated since Java 9, and thus it’s not recommended to use it anymore.

Long(String value) constructor
  • Method declaration – public Long(String s)
  • What does it do? It will take a String argument and constructs a Long instance representing the value indicated by the string.
public class Codekru {

	public static void main(String[] args) {
		String stringValue = "91234567890";
		Long longObject = new Long(stringValue);

		System.out.println("value : " + longObject);
	}
}

Output –

value : 91234567890

The string passed in the argument should only contain the decimal characters with an optional positive or negative sign at the start of the string. If we pass anything else, then the constructor will throw a NumberFormatException.


// Correct ways to call the constructor 

Long val1 = new Long("10")
Long val1 = new Long("+10")
Long val1 = new Long("-10")

// Wrong ways to call the constructor
Long val1 = new Long("ten")
Long val1 = new Long("10twenty")

Internal implementation of the Long(String s) constructor –

 public Long(String s) throws NumberFormatException {
        this.value = parseLong(s, 10);
    }

It internally calls the parseLong() method of the Long class, which we will discuss in another post.

Note: This constructor is also marked as deprecated and thus not recommended to use anymore.

valueOf() method

valueOf() has three overloaded methods, and it is the most preferred way of creating the objects because it provides a better space and time performance.

The valueOf() method accepts a long primitive or a string as an argument and returns a Long instance representing the specified value.

public class Codekru {

	public static void main(String[] args) {

		Long longObject1 = Long.valueOf(123);
		Long longObject2 = Long.valueOf("567");

		System.out.println("value1 : " + longObject1);
		System.out.println("value2 : " + longObject2);
	}
}

Output –

value1 : 123
value2 : 567
Caching in Long Wrapper class

The Long wrapper class caches values ranging from -128 to 127.

So, if we create an object with a value, say 100, creating another instance with the same value (100 ) will not create a new object. Instead, it will be picked from the cache.

Note: The caching will only work if we use the valueOf() method to create objects, not the constructors.

Let’s look at caching with examples.

public class Codekru {

	public static void main(String[] args) {

		Long longObject1 = Long.valueOf(-123);
		Long longObject2 = Long.valueOf(-123);

		if (longObject1 == longObject2) {
			System.out.println("Both objects are same");
		} else {
			System.out.println("Objects aren't same");
		}
	}
}

Output –

Both objects are same
public class Codekru {

	public static void main(String[] args) {

		Long longObject1 = Long.valueOf(-1234);
		Long longObject2 = Long.valueOf(-1234);

		if (longObject1 == longObject2) {
			System.out.println("Both objects are same");
		} else {
			System.out.println("Objects aren't same");
		}
	}
}

Output –

Objects aren't same
  • == operator is used to comparing references of two objects. It returns true if both references are the same. Otherwise, it will return false.
  • In the 1st example, the primitive value -123 lies in the range of -128 to 127. So, longObject2 was picked from the cache and has the same reference as longObject1. That’s why longObject1 == longObject2 returned true.
  • While in 2nd example, it wasn’t picked from the cache, and a new object was created. So, longObject1 == longObject2 returned false.
Methods of the Long wrapper class
Method Name What does it do?
public static int bitCount(long i) It will return the count of one-bits in the two’s complement binary representation of the specified long value.
public static int compare(long x, long y) It compares two long values numerically.
public static int compareUnsigned(long x, long y) It compares two long values while numerically treating the values as unsigned.
public static Long decode(String nm) It decodes a String into a Long.
public static long divideUnsigned(long dividend, long divisor) It returns the unsigned quotient of dividing the first argument by the second, where each argument and the result are interpreted as an unsigned value.
public static Long getLong(String nm) It determines the long value of the system property with the specified name.
public static long highestOneBit(long i) It 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.
public static long lowestOneBit(long i) It returns a long value with at most a single one-bit, in the position of the lowest-order (“rightmost”) one-bit in the specified longvalue.
public static int numberOfLeadingZeros(long i) It 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, or 64 if the value is equal to zero.
public static int numberOfTrailingZeros(long i) It returns the number of zero bits preceding the lowest-order (“rightmost”) one-bit in the two’s complement binary representation of the specified int value, or 64 if the value is equal to zero.
public static long parseLong(String s) It returns the long value represented by the argument in decimal.
public static long parseUnsignedInt(String s) It returns the unsigned long value represented by the argument in decimal.
public static long remainderUnsigned(long dividend, long divisor) It returns the unsigned remainder of the first argument divided by the second.
public static long reverse(long i) It returns the value obtained by reversing the order of the bits in the specified long value.
public static long reverseBytes(long i) It returns the value obtained by reversing the bytes in the specified long value.
public static long rotateLeft(long i, int distance) It returns the value obtained by rotating the two’s complement binary representation of the specified long value left by the specified number of bits.
public static long rotateRight(long i, int distance) It returns the value obtained by rotating the two’s complement binary representation of the specified long value right by the specified number of bits.
public static String toBinaryString(long i) It returns the string representation of the unsigned long value represented by the argument in binary ( base 2 ).
public static String toHexString(long i) It returns the string representation of the unsigned long value represented by the argument in hexadecimal ( base 16 ).
public static String toOctalString(long i) It returns the string representation of the unsigned long value represented by the argument in octal ( base 8 ).
public static long toUnsignedLong(int x) It returns the argument converted to long by an unsigned conversion.
public static String toUnsignedString(int i) It returns an unsigned string representation of the argument.
public static String toString() It returns a string representation of the argument.
public static Long valueOf(long l) It returns a Long instance representing l.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected].

Related Articles
Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *