Skip to main content Skip to docs navigation
TechSpiderTutorials

Java.lang.Long Class in Java

Java.lang.Long Class

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.

Creating Objects

Using a long value:

long longValue = 123456789L;
Long longObject = new Long(longValue);

Using a String representation:

String longString = "9876543210";
Long longObject = Long.valueOf(longString);

Long Class Methods

public byte byteValue()
Returns the value of this Long as a byte after a narrowing primitive conversion.


public short shortValue()
Returns the value of this Long as a short after a narrowing primitive conversion.


public int intValue()
Returns the value of this Long as an int after a narrowing primitive conversion.


public long longValue()
Returns the value of this Long as a long value.


public float floatValue()
Returns the value of this Long as a float after a widening primitive conversion.


public double doubleValue()
Returns the value of this Long as a double after a widening primitive conversion.


public static long max(long a,long b)
Returns the greater of two long values as if by calling Math.max.


public static long min(long a,long b)
Returns the smaller of two long values as if by calling Math.min.


public static long parseLong(String s) throws NumberFormatException
Parses the string argument as a signed decimal long. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' (\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value.


public static String toBinaryString(long i)
Returns a string representation of the long argument as an unsigned integer in base 2.


public static String toHexString(long i)
Returns a string representation of the long argument as an unsigned integer in base 16.


public static String toOctalString(long i)
Returns a string representation of the long argument as an unsigned integer in base 8.

Long Class Example

import java.lang.Long;

public class LongExample {
    public static void main(String[] args) {
        // Creating Long objects
        Long long1 = new Long(123456789L);
        Long long2 = Long.valueOf("987654321");

        // Displaying the values
        System.out.println("Long 1: " + long1);
        System.out.println("Long 2: " + long2);

        // Using Long methods
        long longValue1 = long1.longValue();
        long longValue2 = long2.longValue();

        System.out.println("Long 1 as long: " + longValue1);
        System.out.println("Long 2 as long: " + longValue2);

        // Parsing a long from a string
        String longString = "4567890123";
        long parsedLong = Long.parseLong(longString);
        System.out.println("Parsed long: " + parsedLong);

        // Converting a long to String representation
        String longAsString = Long.toString(7890123456L);
        System.out.println("Long as string: " + longAsString);

        // Getting the hash code of a Long object
        int hashCode = long1.hashCode();
        System.out.println("Hash code of Long 1: " + hashCode);

        // Comparing Long objects
        int comparison = Long.compare(long1, long2);
        System.out.println("Comparison result: " + comparison);

        // Min and Max values of longs
        System.out.println("Minimum value of a long: " + Long.MIN_VALUE);
        System.out.println("Maximum value of a long: " + Long.MAX_VALUE);

        // Converting long to binary, octal, and hexadecimal strings
        long num = 1234567890L;
        System.out.println("Binary string of " + num + ": " + Long.toBinaryString(num));
        System.out.println("Octal string of " + num + ": " + Long.toOctalString(num));
        System.out.println("Hexadecimal string of " + num + ": " + Long.toHexString(num));
    }
}

Output:

Long 1: 123456789
Long 2: 987654321
Long 1 as long: 123456789
Long 2 as long: 987654321
Parsed long: 4567890123
Long as string: 7890123456
Hash code of Long 1: 123456789
Comparison result: -1
Minimum value of a long: -9223372036854775808
Maximum value of a long: 9223372036854775807
Binary string of 1234567890: 1001001100101100000001011010010
Octal string of 1234567890: 11145401322
Hexadecimal string of 1234567890: 499602d2