Java long

Java long – In Java, the long keyword is used to define a variable that holds a whole number with a larger range than int. It is a 64-bit signed two’s complement integer, making it ideal for storing large numerical values.

The long data type is one of Java’s eight primitive data types. It is used when a wider range than what an int provides is needed.

This tutorial covers various aspects of the Java long data type, including:

  1. The type of values a long can store and its size.
  2. How to declare, initialize, and update a long variable.
  3. Maximum and minimum values a long data type can allow.
  4. Printing a long value to the console output.
  5. Performing arithmetic and bitwise operations on long values.
  6. Converting between long and other primitive data types.
  7. Using the Long class for additional operations.

Each section includes detailed descriptions and examples to help you master large integer operations in Java.

1 Number of Bytes for a Long

A long in Java occupies 8 bytes (64 bits) of memory.

2 Range of a Long in Java

Since a long is 64 bits, one bit is reserved for the sign and 63 bits for the value. This gives a range from -263 to 263 – 1, which is from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

3 Declare Variable of Type Long

To declare a long variable, use the following syntax:

</>
Copy
long variable_name;

For example, to declare a long variable named l:

</>
Copy
long l;

4 Initialize Variable with Long Value

You can initialize a long variable by assigning it a value. Note the L suffix which indicates a long literal. For example, to declare and initialize l with the value 1234567890123:

</>
Copy
long l = 1234567890123L;

5 Update a Long Variable

To update an existing long variable, assign it a new value. For example, if l is initially 9876543210L and you want to update it to 112233445566L:

</>
Copy
long l = 9876543210L;
l = 112233445566L;

6 Default Value of a Static Long Variable

Static long variables are automatically initialized to 0 if no explicit value is provided. For example:

</>
Copy
public class Example {
    static long l;
    public static void main(String[] args) {
        System.out.println(l);
    }
}

Output:

0

7 Print Long to Console

To print a long to the console, use the System.out.println() method. For example:

</>
Copy
public class Example {
    public static void main(String[] args) {
        long l = 9223372036854775807L;
        System.out.println(l);
    }
}

Output:

9223372036854775807

8 Always Initialize Local Long Variables

Local variables must be explicitly initialized before use, otherwise the compiler will throw an error. Consider this example:

</>
Copy
public class Example {
    public static void main(String[] args) {
        long l;
        System.out.println(l);
    }
}

Output (Compilation Error):

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The local variable l may not have been initialized
    at Example.main(Example.java:4)

9 Long Maximum Value

You can retrieve the maximum value a long can represent by using the Long.MAX_VALUE constant. For example:

</>
Copy
public class Example {
    public static void main(String[] args) {
        long l = Long.MAX_VALUE;
        System.out.println(l);
    }
}

Output:

9223372036854775807

10 Long Minimum Value

Similarly, use the Long.MIN_VALUE constant to retrieve the minimum value a long can hold:

</>
Copy
public class Example {
    public static void main(String[] args) {
        long l = Long.MIN_VALUE;
        System.out.println(l);
    }
}

Output:

-9223372036854775808

11 Java long – Arithmetic Operations

You can perform various arithmetic operations on long values. For example, consider the following operations on two long variables:

</>
Copy
public class Example {
    public static void main(String[] args) {
        long a = 5000000000L;
        long b = 2000000000L;
        System.out.println("a + b = " + (a + b));
        System.out.println("a - b = " + (a - b));
        System.out.println("a * b = " + (a * b));
        System.out.println("a / b = " + (a / b));
        System.out.println("a % b = " + (a % b));
    }
}

Output:

a + b = 7000000000
a - b = 3000000000
a * b = 10000000000000000000
a / b = 2
a % b = 1000000000

12 Java long – Bitwise Operations

Bitwise operations allow you to manipulate the individual bits of a long value. The example below demonstrates common bitwise operations:

</>
Copy
public class Example {
    public static void main(String[] args) {
        long a = 25L;  // 11001 in binary
        long b = 12L;  // 01100 in binary
        System.out.println("a & b   = " + (a & b));
        System.out.println("a | b   = " + (a | b));
        System.out.println("a ^ b   = " + (a ^ b));
        System.out.println("~a      = " + (~a));
        System.out.println("a << 2  = " + (a << 2));
        System.out.println("a >> 2  = " + (a >> 2));
    }
}

Output:

a & b   = 8
a | b   = 29
a ^ b   = 21
~a      = -26
a << 2  = 100
a >> 2  = 6

13 Converting Long to Other Primitive Data Types

You can convert a long value to other primitive data types using typecasting. For example:

</>
Copy
public class Example {
    public static void main(String[] args) {
        long l = 9876543210L;
        int i = (int) l;      // May lead to loss of data if l is too large
        float f = (float) l;
        double d = (double) l;
        char c = (char) l;    // Conversion may result in an unexpected character
    }
}

14 Converting Other Primitive Data Types to Java long

You can also convert other primitive types to a long using typecasting. Keep in mind that converting from a type with a smaller range might require explicit casting:

</>
Copy
public class Example {
    public static void main(String[] args) {
        int i = 12345;
        long l = (long) i;
        
        float f = 3.14159f;
        l = (long) f;
        
        double d = 98765.4321;
        l = (long) d;
    }
}

Conclusion

In this Java Tutorial, we explored the long data type. We learned how to declare, initialize, and update a long variable; accessed its maximum and minimum values; printed it to the console; performed arithmetic and bitwise operations; converted between different primitive data types; and reviewed the use of the Long class. These fundamental concepts are essential for effectively working with large integer values in Java.