ZetCode

Java Long Class

Last modified: April 13, 2025

The java.lang.Long class is a wrapper class for the primitive type long. It provides methods for converting long values to strings and vice versa, as well as constants and methods useful when working with long values.

The Long class is immutable, meaning its value cannot be changed after creation. It extends the Number class and implements the Comparable interface. This makes it useful in collections and other scenarios where objects are required instead of primitives.

Long Class Methods

The Long class provides numerous static and instance methods for working with long values. Key methods include parsing strings to longs, converting longs to strings, comparing long values, and various bit manipulation operations.

public final class Long extends Number implements Comparable<Long> {
    public static final long MIN_VALUE = 0x8000000000000000L;
    public static final long MAX_VALUE = 0x7fffffffffffffffL;
    
    public static long parseLong(String s) {...}
    public static Long valueOf(long l) {...}
    public static Long valueOf(String s) {...}
    public static String toString(long l) {...}
    public static String toBinaryString(long l) {...}
    public static String toHexString(long l) {...}
    public int compareTo(Long anotherLong) {...}
    public long longValue() {...}
    // ... and many more
}

The code above shows some of the main methods and constants provided by the Long class. These methods allow for convenient manipulation and conversion of long values in various formats.

Creating Long Objects

Long objects can be created using constructors or static factory methods. Since Java 9, constructors are deprecated in favor of valueOf methods, which may return cached objects for better performance.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // Using constructor (deprecated since Java 9)
        Long num1 = new Long(123456789L);
        
        // Using valueOf method (preferred)
        Long num2 = Long.valueOf(987654321L);
        
        // Parsing from String
        Long num3 = Long.valueOf("123456789012345");
        
        // Using autoboxing
        Long num4 = 98765432109876L;
        
        System.out.println("num1: " + num1);
        System.out.println("num2: " + num2);
        System.out.println("num3: " + num3);
        System.out.println("num4: " + num4);
    }
}

This example demonstrates different ways to create Long objects. The valueOf methods are preferred over constructors as they may use cached instances for better performance. Autoboxing automatically converts primitive longs to Long objects.

Parsing Strings to Longs

The Long class provides several methods for converting strings to long values. The parseLong method converts a string to a primitive long, while valueOf returns a Long object. Both can handle different radixes (number bases).

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // Parse decimal string
        long decimal = Long.parseLong("123456789");
        System.out.println("Decimal: " + decimal);
        
        // Parse hexadecimal string
        long hex = Long.parseLong("1A3F", 16);
        System.out.println("Hex 1A3F: " + hex);
        
        // Parse binary string
        long binary = Long.parseLong("101010", 2);
        System.out.println("Binary 101010: " + binary);
        
        // Using valueOf which returns Long object
        Long octal = Long.valueOf("777", 8);
        System.out.println("Octal 777: " + octal);
        
        try {
            Long invalid = Long.parseLong("123ABC");
        } catch (NumberFormatException e) {
            System.out.println("Error parsing: " + e.getMessage());
        }
    }
}

This example shows how to parse strings into long values using different number bases. The parseLong method throws NumberFormatException for invalid inputs. The radix parameter (2-36) specifies the number base for conversion.

Converting Longs to Strings

Long provides multiple methods to convert long values to strings in various formats. These include standard decimal strings, as well as binary, octal, and hexadecimal representations. The toString method is the most commonly used.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        long number = 255L;
        
        // Standard decimal string
        String decimalStr = Long.toString(number);
        System.out.println("Decimal: " + decimalStr);
        
        // Binary string
        String binaryStr = Long.toBinaryString(number);
        System.out.println("Binary: " + binaryStr);
        
        // Hexadecimal string
        String hexStr = Long.toHexString(number);
        System.out.println("Hex: " + hexStr);
        
        // Octal string
        String octalStr = Long.toOctalString(number);
        System.out.println("Octal: " + octalStr);
        
        // String with specified radix
        String base5Str = Long.toString(number, 5);
        System.out.println("Base 5: " + base5Str);
    }
}

This example demonstrates various string conversion methods available in the Long class. Each method produces a string representation of the number in the specified base. The toString method can handle any radix from 2 to 36.

Comparing Long Values

The Long class provides several ways to compare long values. The compare and compareTo methods perform numerical comparison, while equals checks for object equality. There are also methods for unsigned comparison.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        Long a = 100L;
        Long b = 200L;
        Long c = 100L;
        
        // Using compareTo
        System.out.println("a.compareTo(b): " + a.compareTo(b));
        System.out.println("a.compareTo(c): " + a.compareTo(c));
        
        // Using static compare
        System.out.println("Long.compare(a, b): " + Long.compare(a, b));
        
        // Using equals
        System.out.println("a.equals(b): " + a.equals(b));
        System.out.println("a.equals(c): " + a.equals(c));
        
        // Unsigned comparison
        long x = -1L;
        long y = 1L;
        System.out.println("Unsigned compare: " + Long.compareUnsigned(x, y));
    }
}

This example shows different comparison techniques for Long values. The compareTo and compare methods return negative, zero, or positive values indicating the relationship between numbers. The compareUnsigned method treats values as unsigned for comparison.

Bit Manipulation Methods

The Long class includes several static methods for bit-level manipulation of long values. These include counting bits, reversing bits, rotating bits, and finding highest/lowest set bit. They're useful for low-level programming.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        long num = 0b10101010101010101010101010101010L;
        
        System.out.println("Number: " + Long.toBinaryString(num));
        System.out.println("Bit count: " + Long.bitCount(num));
        System.out.println("Highest one bit: " + 
            Long.toBinaryString(Long.highestOneBit(num)));
        System.out.println("Lowest one bit: " + 
            Long.toBinaryString(Long.lowestOneBit(num)));
        System.out.println("Number of leading zeros: " + 
            Long.numberOfLeadingZeros(num));
        System.out.println("Number of trailing zeros: " + 
            Long.numberOfTrailingZeros(num));
        System.out.println("Reversed bits: " + 
            Long.toBinaryString(Long.reverse(num)));
        System.out.println("Rotated left by 4: " + 
            Long.toBinaryString(Long.rotateLeft(num, 4)));
    }
}

This example demonstrates various bit manipulation methods. The bitCount method counts set bits, highestOneBit finds the leftmost set bit, and reverse flips all bits. These operations are performed at the binary level on the long value.

Long Constants and Size Methods

The Long class defines several useful constants and methods related to the size and limits of the long type. These include MIN_VALUE, MAX_VALUE, SIZE, and BYTES, which provide information about the long data type's characteristics.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        System.out.println("Long min value: " + Long.MIN_VALUE);
        System.out.println("Long max value: " + Long.MAX_VALUE);
        System.out.println("Size in bits: " + Long.SIZE);
        System.out.println("Size in bytes: " + Long.BYTES);
        
        long num = 1234567890123456789L;
        System.out.println("Signum of " + num + ": " + Long.signum(num));
        System.out.println("Signum of -" + num + ": " + Long.signum(-num));
        System.out.println("Signum of 0: " + Long.signum(0));
        
        System.out.println("Hash code of " + num + ": " + Long.hashCode(num));
    }
}

This example shows the use of Long class constants and utility methods. The MIN_VALUE and MAX_VALUE constants define the range of valid long values. The signum method returns -1, 0, or 1 indicating the number's sign. The hashCode method returns a hash code for the long value.

Source

Java Long Class Documentation

In this article, we've covered the essential methods of the Java Long class with practical examples. The Long class provides robust functionality for working with long values, including parsing, conversion, comparison, and bit operations.

Author

My name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts.

List all Java tutorials.