Long Wrapper Class in Java

The Long class wraps a value of the primitive type long in an object. An object of type Long contains a single field whose type is long.
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 Constructors

  • 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.
Example:
long l = 55;
String str = "45";

// Construct two Long objects
Long x = new Long(l);
Long y = new Long(str);

Long class Methods

A diagram shows a list of APIs/Methods that Long class provides.
In this post, we will learn a few important methods of the Long wrapper class.

toString() methods

  • 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.
Example:
private static void toStringMethods() {
 // toString()
 System.out.println("toString(b) = " + Long.toString(l));

 // toHexString(),toOctalString(),toBinaryString()
 // converts into hexadecimal, octal and binary forms.
 System.out.println("toHexString(b) =" + Long.toHexString(l));
 System.out.println("toOctalString(b) =" + Long.toOctalString(l));
 System.out.println("toBinaryString(b) =" + Long.toBinaryString(l));

}

valueOf() methods

  • 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, in - radix) - Returns a Long object holding the value extracted from the specified String when parsed with the radix given by the second argument.
Example:
private static void valueOfMethods() {
 // valueOf(): return Long object
 // an overloaded method takes radix as well.
 Long z = Long.valueOf(l);
 System.out.println("valueOf(b) = " + z);
 z = Long.valueOf(str);
 System.out.println("ValueOf(bb) = " + z);
 z = Long.valueOf(str, 6);
 System.out.println("ValueOf(bb,6) = " + z);
}

parseLong() methods

  • 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.
Example:
private static void parseLongMethods() {
 // parseLong(): return primitive long value
 // an overloaded method takes radix as well
 long zz = Long.parseLong(str);
 System.out.println("parseLong(bb) = " + zz);
 zz = Long.parseLong(str, 6);
 System.out.println("parseLong(bb,6) = " + zz);

}

rotateLeft() and rotateRight() methods

  • 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.
Example:
private static void rotateMethods() {
 // rotateLeft and rotateRight can be used
 // to rotate bits by specified distance
 long valrot = 2;
 System.out.println("rotateLeft(0000 0000 0000 0010 , 2) =" + Long.rotateLeft(valrot, 2));
 System.out.println("rotateRight(0000 0000 0000 0010,3) =" + Long.rotateRight(valrot, 3));

}

decode() method

  • static Long decode(String nm) - Decodes a String into a Long
private static void decodeMethods() {

 // decode() : decodes the hex,octal and decimal
 // string to corresponding long values.
 String decimal = "45";
 String octal = "005";
 String hex = "0x0f";

 Long dec = Long.decode(decimal);
 System.out.println("decode(45) = " + dec);
 dec = Long.decode(octal);
 System.out.println("decode(005) = " + dec);
 dec = Long.decode(hex);
 System.out.println("decode(0x0f) = " + dec);

}

getLong() method

  • 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.
Example:
private static void getLongMethods() {
 // getLong(): can be used to retrieve
 // long value of system property
 long prop = Long.getLong("sun.arch.data.model");
 System.out.println("getLong(sun.arch.data.model) = " + prop);
 System.out.println("getLong(abcd) =" + Long.getLong("abcd"));

 // an overloaded getLong() method
 // which return default value if property not found.
 System.out.println("getLong(abcd,10) =" + Long.getLong("abcd", 10));
}

intValue,byteValue,shortValue,longValue,doubleValue,floatValue() methods

private static void valueMethods() {
 Long x = new Long(l);
 // xxxValue can be used to retrieve
 // xxx type value from long value.
 // xxx can be int,byte,short,long,double,float
 System.out.println("bytevalue(x) = " + x.byteValue());
 System.out.println("shortvalue(x) = " + x.shortValue());
 System.out.println("intvalue(x) = " + x.intValue());
 System.out.println("longvalue(x) = " + x.longValue());
 System.out.println("doublevalue(x) = " + x.doubleValue());
 System.out.println("floatvalue(x) = " + x.floatValue());
}
There few more methods, refer the comments in below program are self descriptive.
public static void main(String[] args) {
 long l = 55;
 String str = "45";

 // Construct two Long objects
 Long x = new Long(l);
 Long y = new Long(str);

 long value = 45;

 // bitcount() : can be used to count set bits
 // in twos complement form of the number
 System.out.println("Long.bitcount(value)=" + Long.bitCount(value));

 // numberOfTrailingZeroes and numberOfLeaadingZeroes
 // can be used to count prefix and postfix sequence of 0
 System.out.println("Long.numberOfTrailingZeros(value)=" + Long.numberOfTrailingZeros(value));
 System.out.println("Long.numberOfLeadingZeros(value)=" + Long.numberOfLeadingZeros(value));

 // highestOneBit returns a value with one on highest
 // set bit position
 System.out.println("Long.highestOneBit(value)=" + Long.highestOneBit(value));

 // highestOneBit returns a value with one on lowest
 // set bit position
 System.out.println("Long.lowestOneBit(value)=" + Long.lowestOneBit(value));

 // reverse() can be used to reverse order of bits
 // reverseytes() can be used to reverse order of bytes
 System.out.println("Long.reverse(value)=" + Long.reverse(value));
 System.out.println("Long.reverseBytes(value)=" + Long.reverseBytes(value));

 // signum() returns -1,0,1 for negative,0 and positive
 // values
 System.out.println("Long.signum(value)=" + Long.signum(value));

 // hashcode() returns hashcode of the object
 int hash = x.hashCode();
 System.out.println("hashcode(x) = " + hash);

 // equals returns boolean value representing equality
 boolean eq = x.equals(y);
 System.out.println("x.equals(y) = " + eq);

 // compare() used for comparing two int values
 int e = Long.compare(x, y);
 System.out.println("compare(x,y) = " + e);

 // compareTo() used for comparing this value with some
 // other value
 int f = x.compareTo(y);
 System.out.println("x.compareTo(y) = " + f);
}
Output:
toString(b) = 55
toHexString(b) =37
toOctalString(b) =67
toBinaryString(b) =110111
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15
getLong(sun.arch.data.model) = 64
getLong(abcd) =null
getLong(abcd,10) =10
parseLong(bb) = 45
parseLong(bb,6) = 29
rotateLeft(0000 0000 0000 0010 , 2) =8
rotateRight(0000 0000 0000 0010,3) =4611686018427387904
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.0
floatvalue(x) = 55.0
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
Long.bitcount(value)=4
Long.numberOfTrailingZeros(value)=0
Long.numberOfLeadingZeros(value)=58
Long.highestOneBit(value)=32
Long.lowestOneBit(value)=1
Long.reverse(value)=-5476377146882523136
Long.reverseBytes(value)=3242591731706757120
Long.signum(value)=1
hashcode(x) = 55
x.equals(y) = false
compare(x,y) = 1
x.compareTo(y) = 1

Complete Program for Reference

package com.javaguides.corejava.wrapperclasses;

public class LongClassExample {

 static long l = 55;
 static String str = "45";

 private static void toStringMethods() {
  // toString()
  System.out.println("toString(b) = " + Long.toString(l));

  // toHexString(),toOctalString(),toBinaryString()
  // converts into hexadecimal, octal and binary forms.
  System.out.println("toHexString(b) =" + Long.toHexString(l));
  System.out.println("toOctalString(b) =" + Long.toOctalString(l));
  System.out.println("toBinaryString(b) =" + Long.toBinaryString(l));

 }

 private static void valueOfMethods() {
  // valueOf(): return Long object
  // an overloaded method takes radix as well.
  Long z = Long.valueOf(l);
  System.out.println("valueOf(b) = " + z);
  z = Long.valueOf(str);
  System.out.println("ValueOf(bb) = " + z);
  z = Long.valueOf(str, 6);
  System.out.println("ValueOf(bb,6) = " + z);
 }

 private static void parseLongMethods() {
  // parseLong(): return primitive long value
  // an overloaded method takes radix as well
  long zz = Long.parseLong(str);
  System.out.println("parseLong(bb) = " + zz);
  zz = Long.parseLong(str, 6);
  System.out.println("parseLong(bb,6) = " + zz);

 }

 private static void getLongMethods() {
  // getLong(): can be used to retrieve
  // long value of system property
  long prop = Long.getLong("sun.arch.data.model");
  System.out.println("getLong(sun.arch.data.model) = " + prop);
  System.out.println("getLong(abcd) =" + Long.getLong("abcd"));

  // an overloaded getLong() method
  // which return default value if property not found.
  System.out.println("getLong(abcd,10) =" + Long.getLong("abcd", 10));
 }

 private static void decodeMethods() {

  // decode() : decodes the hex,octal and decimal
  // string to corresponding long values.
  String decimal = "45";
  String octal = "005";
  String hex = "0x0f";

  Long dec = Long.decode(decimal);
  System.out.println("decode(45) = " + dec);
  dec = Long.decode(octal);
  System.out.println("decode(005) = " + dec);
  dec = Long.decode(hex);
  System.out.println("decode(0x0f) = " + dec);

 }

 private static void rotateMethods() {
  // rotateLeft and rotateRight can be used
  // to rotate bits by specified distance
  long valrot = 2;
  System.out.println("rotateLeft(0000 0000 0000 0010 , 2) =" + Long.rotateLeft(valrot, 2));
  System.out.println("rotateRight(0000 0000 0000 0010,3) =" + Long.rotateRight(valrot, 3));

 }

 private static void valueMethods() {
  Long x = new Long(l);
  // xxxValue can be used to retrieve
  // xxx type value from long value.
  // xxx can be int,byte,short,long,double,float
  System.out.println("bytevalue(x) = " + x.byteValue());
  System.out.println("shortvalue(x) = " + x.shortValue());
  System.out.println("intvalue(x) = " + x.intValue());
  System.out.println("longvalue(x) = " + x.longValue());
  System.out.println("doublevalue(x) = " + x.doubleValue());
  System.out.println("floatvalue(x) = " + x.floatValue());
 }

 public static void main(String[] args) {
  long l = 55;
  String str = "45";

  // Construct two Long objects
  Long x = new Long(l);
  Long y = new Long(str);

  toStringMethods();
  decodeMethods();
  getLongMethods();
  parseLongMethods();
  rotateMethods();
  valueMethods();
  valueOfMethods();
  
  
  long value = 45;

  // bitcount() : can be used to count set bits
  // in twos complement form of the number
  System.out.println("Long.bitcount(value)=" + Long.bitCount(value));

  // numberOfTrailingZeroes and numberOfLeaadingZeroes
  // can be used to count prefix and postfix sequence of 0
  System.out.println("Long.numberOfTrailingZeros(value)=" + Long.numberOfTrailingZeros(value));
  System.out.println("Long.numberOfLeadingZeros(value)=" + Long.numberOfLeadingZeros(value));

  // highestOneBit returns a value with one on highest
  // set bit position
  System.out.println("Long.highestOneBit(value)=" + Long.highestOneBit(value));

  // highestOneBit returns a value with one on lowest
  // set bit position
  System.out.println("Long.lowestOneBit(value)=" + Long.lowestOneBit(value));

  // reverse() can be used to reverse order of bits
  // reverseytes() can be used to reverse order of bytes
  System.out.println("Long.reverse(value)=" + Long.reverse(value));
  System.out.println("Long.reverseBytes(value)=" + Long.reverseBytes(value));

  // signum() returns -1,0,1 for negative,0 and positive
  // values
  System.out.println("Long.signum(value)=" + Long.signum(value));

  // hashcode() returns hashcode of the object
  int hash = x.hashCode();
  System.out.println("hashcode(x) = " + hash);

  // equals returns boolean value representing equality
  boolean eq = x.equals(y);
  System.out.println("x.equals(y) = " + eq);

  // compare() used for comparing two int values
  int e = Long.compare(x, y);
  System.out.println("compare(x,y) = " + e);

  // compareTo() used for comparing this value with some
  // other value
  int f = x.compareTo(y);
  System.out.println("x.compareTo(y) = " + f);
 }

}
Output:
toString(b) = 55
toHexString(b) =37
toOctalString(b) =67
toBinaryString(b) =110111
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15
getLong(sun.arch.data.model) = 64
getLong(abcd) =null
getLong(abcd,10) =10
parseLong(bb) = 45
parseLong(bb,6) = 29
rotateLeft(0000 0000 0000 0010 , 2) =8
rotateRight(0000 0000 0000 0010,3) =4611686018427387904
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.0
floatvalue(x) = 55.0
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
Long.bitcount(value)=4
Long.numberOfTrailingZeros(value)=0
Long.numberOfLeadingZeros(value)=58
Long.highestOneBit(value)=32
Long.lowestOneBit(value)=1
Long.reverse(value)=-5476377146882523136
Long.reverseBytes(value)=3242591731706757120
Long.signum(value)=1
hashcode(x) = 55
x.equals(y) = false
compare(x,y) = 1
x.compareTo(y) = 1

References

https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html

Related Wrapper Classes

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments