Java Long Class | Methods, Example

Last Updated on February 8, 2021 by Scientech Easy

Long class in Java is a wrapper class that wraps (converts) a value of primitive data type “long” in an object. In simple words, an Integer is a wrapper around a long.

An object of Long class contains a single field of type long. In this field, we can store a primitive long value.

Long class was introduced in JDK 1.0. It is present in java.lang.Long package.

Java Long class declaration


The general declaration of Long class in Java is as follows:

public final class Long
   extends Number
      implements Comparable<Long>

Long class extends Number class and implements Serializable and Comparable<Long> interfaces. Number class extends Object class.

Long Field Constants


Java Long class provides several field constants that are as follows:

1. static int BYTES: It is the number of bytes that are used to represent a long value in two’s complement binary form. It was introduced in JDK 1.8 version.

2. static long MAX_VALUE: It represents a constant that can hold the maximum value of long (2^63 – 1).

3. static long MIN_VALUE: It represents a constant that can hold the minimum value of long (-2^63).

4. static int SIZE: It is the number of bits that are used to represent a long value in two’s complement binary form. It was introduced in JDK 1.5.

5. static Class<Long> TYPE: It is a Class instance that represents the primitive type long. It was introduced in JDK 1.1 version.

Java Long Class Constructor


Long class in java defines two constructors that are as follows:

1. Long(long num): This form of constructor accepts long number as its parameter and converts it into a Long class object. The general syntax to create Long class object is as follows:

Long obj = new Long(12300055); // Here, we are converting a primitive long value into Long class object. This is called boxing in java.

2. Long(String str): This constructor accepts a parameter of String type and converts that string into Long class object. The string contains a long value.


The general syntax to create a Long object by wrapping a string that contains a long number, as follows:

String str = "12300055";
   Long obj = new Long(str);

Important Long Class Methods in Java


In addition to methods inherited from Number class and Object class, Java Long class also provides some useful important methods. They are as follows:

1. int compareTo(Long obj): This method is used to compare the values of two Long class objects numerically. It is called using the below syntax:

int x = obj1.compareTo(obj2);

where, obj1 and obj2 are Long class objects.

  • If obj1 == obj2, this method returns 0.
  • If obj1 < obj2, it returns a negative value.
  • If obj1 > obj2, it returns positive value.

2. boolean equals(Object obj): This method is used to compare Long object with another Long object obj. If both have the same content, this method returns true otherwise returns false.

3. static long parseInt(String str): This method returns the primitive long value contained in the string argument str.

4. String toString(): The toString() method converts Long object into String object and returns that String object. It returns a string form of Long object.

5. static Long valueOf(String str): It converts string str containing some long value into Long class object and returns that Long object. In other words, it returns a Long object holding a value given by the specified String.

6. static Long valueOf(long l): This method converts the primitive long value into Long object. In other words, it returns a Long object representing the specified long value.

7. static Long valueOf​(String s): It returns a Long object that contains the value of the specified String.

8. static int compare(long x, long y): It is used to compare two long values numerically.

9. static int compareUnsigned​(long x, long y): This method compares two long values numerically and treats those values as unsigned.

10. static Long decode(String str): This method decodes a String into a Long object.


11. static String toBinaryString(long l): This method is used to convert a decimal long value l into binary number system. After converting, it returns that binary number in the form of string.

12. static String toHexString(long l): This method is used to convert a decimal long value l into hexadecimal number system. After converting, it returns that hexadecimal number in the form of string.

13. static String toOctalString(long l): This method is used to convert a decimal long value l into octal number system. After converting, it returns that octal number in the form of a string.

14. static Long valueOf(long l): It returns a Long object that contains the specified long value l.

15. static int signum(long l): This method returns the signum function of the specified long value l. It returns -1 for negative values, 0 for 0 and +1 for values greater than 0.

16. static long reverse(long l): This method returns the value obtained by reversing the order of the bits in the two’s complement binary representation of the specified long value l.

17. static long reverseBytes​(long l): This method returns the value obtained by reversing the order of bytes in the two’s complement representation of the specified long value l.

18. static int bitCount​(long l): This method returns the number of one-bits in the two’s complement binary representation of the specified long value l.

19. int hashCode(): It returns a hash code value for the Long object.

20. static long parseUnsignedLong​(String s): It parses the string argument s as an unsigned decimal long form.

21. static long sum​(long x, long y): This method adds two long values together by using + operator.

22. static String toUnsignedString​(long l): This method returns a string representation of the argument as an unsigned decimal long value.

Example Program based on Long Class Methods


Let’s take an example program where we will accept a number from a keyboard and convert it into decimal, binary, hexadecimal, and octal number systems by using methods provided by the Long class in java.

Look at the below source code to understand better.

Program source code 1:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class LongTest {
public static void main(String[] args) throws IOException
{
// Accept an Integer number from keyboard. 	
  InputStreamReader sr = new InputStreamReader(System.in);
  BufferedReader bf = new BufferedReader(sr);
	 
 System.out.println("Enter an integer number:");
  String str = bf.readLine();
 
// Converting string into long type.
  long l = Long.parseLong(str);
  System.out.println("In decimal format: " +l);

// Converting into other number systems.
   str = Long.toBinaryString(l);
   System.out.println("In binary format: " +str);
  
   str = Long.toHexString(l);
   System.out.println("In hexdecimal format: " +str);
  
   str = Long.toOctalString(l);
   System.out.println("In octal format: " +str);
  }
}
Output:
       Enter an integer number: 123456789
       In decimal format: 123456789
       In binary format: 111010110111100110100010101
       In hexdecimal format: 75bcd15
       In octal format: 726746425

2. Let’s take another example program based on the above methods provided by the Long class.

Program source code 2:

public class LongTest {
public static void main(String[] args)
{
 long x = 12300055; 
 String xx = "12300070"; 
   
// Create two Long class objects. 
  Long l1 = new Long(x); 
  Long l2 = new Long(xx); 

 System.out.println("byteValue(): " + l1.byteValue()); 
 System.out.println("shortValue(): " + l1.shortValue()); 
 System.out.println("intValue(): " + l1.intValue()); 
 System.out.println("longValue(): " + l1.longValue()); 
 System.out.println("doubleValue(): " + l1.doubleValue()); 
 System.out.println("floatValue(): " + l1.floatValue());
 
 long value = 12300060; 
 
// Call bitcount() method to count set of bits in twos complement form of the number.
   System.out.println("bitcount(value): " + Long.bitCount(value));  
   
// Call reverse() method to reverse order of bits. 
  System.out.println("reverse(value): " +Long.reverse(value)); 

// Call reverseBytes() method to reverse order of bytes.
   System.out.println("reverseBytes(value): " +Long.reverseBytes(value)); 
     
// Call signum() method that returns -1,0,1 for negative, 0 and positive values. 
   System.out.println("signum(value): " + Long.signum(value)); 
 
// hashcode() returns hashcode of the object 
   int hash = l1.hashCode(); 
   System.out.println("hashcode(): " + hash); 

// Call equals() method that will return a boolean value representing equality. 
  boolean eq = l1.equals(l2); 
   System.out.println("l1.equals(l2): " + eq); 

// Call compare() method used for comparing two long values.
   int comp = Long.compare(l1, l2); 
   System.out.println("compare(l1,l2): " +comp); 

// Call compareTo() method for comparing this value with some another value. 
   int compareTo = l1.compareTo(l2); 
   System.out.println("l1.compareTo(l2):  " + compareTo);   
  }
}
Output:
       byteValue(): 23
       shortValue(): -20713
       intValue(): 12300055
       longValue(): 12300055
       doubleValue(): 1.2300055E7
       floatValue(): 1.2300055E7

       bitcount(value): 15
       reverse(value): 4104429627487813632
       reverseBytes(value): 2067076362660741120
       signum(value): 1
       hashcode(): 12300055

       l1.equals(l2): false
       compare(l1,l2): -1
       l1.compareTo(l2):  -1

Hope that this tutorial has covered the important points related to Long class in Java with example programs. I hope that you will have understood this simple topic.
Thanks for reading!!!