Home› Java Programming Language› / Programs / Java Program to Demonstrate the long Data Type
🚀 Programming Example

Java Program to Demonstrate the long Data Type

2,279 Views
Java Java 8 or later
Beginner
Demonstration

Learn how to declare, initialise and print long variables in Java. This program explains the L suffix, prints the range using Long.MAX_VALUE and Long.MIN_VALUE, and shows how long fixes int overflow in large calculations.

Before You Begin

What you will learn

  • Declare and initialise variables of the long data type
  • Know that long uses 8 bytes (64 bits) and stores values up to about ±9.22 × 1018
  • Understand when the L suffix is optional and when it's required
  • Print the range using Long.MAX_VALUE and Long.MIN_VALUE
  • Use long to avoid int overflow in calculations
  • Understand that long can also overflow
  • Know when to choose long instead of int

Prerequisites

  • JDK (Java Development Kit) installed on your computer
  • Basic structure of a Java program: a class and the main() method
  • How to print output with System.out.println()
  • What a variable is and how to declare one
  • The int data type and its range
  • Basic idea of integer overflow
No previous program
No next program

📌 Information & Algorithm

Algorithm

  1. Start
  2. Create a public class named LongDataType.
  3. Define the main() method.
  4. Declare long variable a and assign it 100000L.
  5. Declare long variable b and assign it -200000L.
  6. Display the values of a and b.
  7. Display Long.MAX_VALUE and Long.MIN_VALUE.
  8. Add a and b, store the result in sum, and display it.
  9. Multiply 50000 * 50000 as int values and display the overflowed result.
  10. Multiply 50000L * 50000 as long values and display the correct result.
  11. Add 1 to Long.MAX_VALUE and display the overflowed result.
  12. Declare worldPopulation as 8_000_000_000L and display it.
  13. Stop

How to Run

Environment: JDK (Windows, Linux, macOS)

Execution command:

javac LongDataType.java
java LongDataType

💻 Program Code

public class LongDataType {

    public static void main(String[] args) {

        // Declare and initialise long variables
        // The 'L' suffix tells Java the literal is a long, not an int
        long a = 100000L;
        long b = -200000L;

        System.out.println("a = " + a);
        System.out.println("b = " + b);

        // Range of the long data type
        System.out.println("Max long value = " + Long.MAX_VALUE);
        System.out.println("Min long value = " + Long.MIN_VALUE);

        // long + long stays a long
        long sum = a + b;
        System.out.println("sum = " + sum);

        // int multiplication overflows, long multiplication does not
        int intResult = 50000 * 50000;
        long longResult = 50000L * 50000;
        System.out.println("int result  (50000 * 50000)  = " + intResult);
        System.out.println("long result (50000L * 50000) = " + longResult);

        // long can overflow too, it just has a much bigger limit
        long overflow = Long.MAX_VALUE + 1;
        System.out.println("Long.MAX_VALUE + 1 = " + overflow);

        // Values above the int range need the L suffix
        long worldPopulation = 8_000_000_000L;
        System.out.println("worldPopulation = " + worldPopulation);
    }
}

Sample Input

No input required

🖥 Program Output

                                a = 100000
b = -200000
Max long value = 9223372036854775807
Min long value = -9223372036854775808
sum = -100000
int result  (50000 * 50000)  = -1794967296
long result (50000L * 50000) = 2500000000
Long.MAX_VALUE + 1 = -9223372036854775808
worldPopulation = 8000000000
                            

📘 Explanation

Overview

long is the largest integer data type in Java. It stores whole numbers in 8 bytes (64 bits) of memory, with a range of about ±9.22 × 1018. Use it when values can go beyond the int limit of about 2.1 billion, for example with timestamps, file sizes, bank transaction IDs or population counts.

Java treats every whole-number literal as an int by default. Add L at the end to make it a long. Use a capital L, because a lowercase l looks like the digit 1.

Step-by-Step Explanation

1

Declare and Initialise long Variables

The L suffix marks a long literal.

long a = 100000L;
long b = -200000L;

These values are small enough to fit in an int, so the L is optional here. It's still good practice, because it shows clearly that the value is meant to be a long.

2

Print the Values

The suffix isn't printed.

System.out.println("a = " + a);
System.out.println("b = " + b);

The output is a = 100000 and b = -200000. The L is only part of the source code.

3

Range of long

The largest and smallest values.

System.out.println("Max long value = " + Long.MAX_VALUE);
System.out.println("Min long value = " + Long.MIN_VALUE);

Long is the wrapper class for long. With 64 bits, a long can store $$2^{64}$$ different values, from $$-2^{63}$$ to $$2^{63} - 1$$.

4

long + long Stays a long

No cast is needed.

long sum = a + b;
System.out.println("sum = " + sum);

Adding two long values gives a long, so the result -100000 is stored directly.

5

How long Fixes int Overflow

The type of the calculation matters.

int intResult = 50000 * 50000;
long longResult = 50000L * 50000;

The correct answer is $$50000 \times 50000 = 2{,}500{,}000{,}000$$, which is bigger than the int maximum. In the first line, both numbers are int, so the result overflows to -1794967296. In the second line, 50000L is a long, so Java does the whole calculation as long and gets the right answer.

6

long Can Overflow Too

The limit is bigger, not unlimited.

long overflow = Long.MAX_VALUE + 1;

Going past the maximum wraps around to the minimum, without any error: $$9223372036854775807 + 1 \rightarrow -9223372036854775808$$ For numbers bigger than this, use BigInteger.

7

Large Literals Need the L Suffix

Here the L is required.

long worldPopulation = 8_000_000_000L;

8 billion is above the int range. Without L, Java treats it as an int and gives the error integer number too large. The underscores only improve readability and don't appear in the output.

long at a Glance

Property Value
Size 8 bytes (64 bits)
Range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Default value (fields) 0L
Literal suffix L (or l, not recommended)
Wrapper class Long

Java Integer Types Compared

Type Size Range Suffix
byte 1 byte -128 to 127 None
short 2 bytes -32,768 to 32,767 None
int 4 bytes -2,147,483,648 to 2,147,483,647 None
long 8 bytes About ±9.22 × 1018 L

Output

a = 100000
b = -200000
Max long value = 9223372036854775807
Min long value = -9223372036854775808
sum = -100000
int result  (50000 * 50000)  = -1794967296
long result (50000L * 50000) = 2500000000
Long.MAX_VALUE + 1 = -9223372036854775808
worldPopulation = 8000000000

Common Mistakes

Missing L on a large literal long x = 8000000000; gives the error integer number too large, even though the variable is a long.
Overflowing before assignment long x = 50000 * 50000; still gives a wrong negative number. The multiplication happens in int first, and only then is the result stored in the long.
Using a lowercase l 100l is easy to misread as 1001. Always use a capital L.
Good practice Put the L on the first number in a calculation, like 50000L * 50000, so the whole expression runs as long.

Key Takeaway

long stores whole numbers in 8 bytes, up to about ±9.22 × 1018. Add L to long literals, and make sure the calculation itself runs as long, or it can overflow before the value is stored.

Complexity

Time: O(1)

Space: O(1)

No previous program
No next program
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.