Java long Keyword

The Java long keyword is a primitive data type. It is used to declare variables. It can also be used with methods. It can hold a 64-bit two's complement integer.

It is commonly used when we need to store large integer values that exceed the range of the int data type. For example, if we need to represent very large numbers or timestamps, long would be appropriate.

Long increases its capacity by supporting 64-bit signed integers, in contrast to its equivalent, int, which can only handle 32-bit signed integers. Because of its wider range, it can represent much larger numbers, which makes it invaluable in situations where more precision and capacity are needed than what int can provide. For Java developers who want to handle big amounts of integer data in their applications, the long data type is a handy tool for handling timestamps, unique identifiers, and computations involving enormous numbers.

Points to Remember

  • The long contains minimum value of -263 and a maximum value of 263-1.
  • From Java 8, the long can represent as an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1
  • Its default value is 0L.
  • Its default size is 8 byte.
  • It is used when you need a higher range integer value.

Applications of long Keyword

  1. Date Representation and Timestamps: Long is frequently used to store timestamps because of its wide range, particularly when working with time measurements or representing dates that fall outside of the usual range of int.
  2. File Handling: Long is frequently used in file I/O operations to handle file sizes, offsets, and locations, especially when files are larger than 2GB, which would be larger than an int's limit.
  3. Mathematical Computations: Long offers the required range and accuracy when carrying out large-number mathematical computations, such as factorials, exponentiation, or operations needing high precision.
  4. Unique Identifiers: Long can be used to create primary keys or unique identifiers in databases, particularly in distributed systems where it's necessary to reduce the chance of a collision over a big number space.
  5. Optimizing Performance: In some cases, using long instead of other numeric types may offer performance benefits, particularly in algorithms or applications that heavily rely on integer arithmetic and require the extended range provided by long.

Examples of Java long keyword

Example 1

Let's see an example of using long data types with positive and negative values.

File name: LongExample1.java

Output:

num1: 10
num2: -10

Explanation

The program's entry point, the main method, is defined in a class called LongExample1 in the Java code that is provided. Two long-type variables, num1 and num2, are defined and initialised within this procedure. A positive long integer, 10L, is allocated to the variable num1, and a negative long integer, -10L, is assigned to num2. Then, using System.out.println, the programme outputs the values of num1 and num2 to the console along with explanatory text. This sample shows how to store huge integer values, including both positive and negative numbers, in Java using the long data type.

Example 2

Let's see an example to check whether the long data type holds decimal value.

File name: classLongExample2.java

Output:

LongExample2.java:5: error: incompatible types: possible lossy conversion from double to long
long num=10.5;  
         ^
1 error

Explanation

The provided Java code causes a compilation error because the long data type can only carry integer values-not decimal numbers-and tries to assign a decimal value (10.5) to a variable num of type long. Long is a primitive data type in Java that is used to represent signed 64-bit integers. Use data types that allow floating-point numbers, such as double or float, to store decimal values. If accuracy loss is acceptable, this mistake can be fixed by casting the decimal value to an integer type or by changing the data type of num to double or float.

Example 3

Let's see an example to check whether the long data type holds float value.

File name: LongExample3.java

Output:

LongExample3.java:5: error: incompatible types: possible lossy conversion from float to long
long num=10f;  
         ^
1 error

Explanation

There is a compilation problem in the submitted Java code because it tries to assign a floating-point value (10f) to a variable num of type long.

A variable called num is declared and initialized with the value 10f inside the main() function. However, Java does not allows us to assign a floating-point value to a primitive data type like long. Instead, long is meant to carry integer values.

Example 4

Let's see an example to check whether the long data type holds char value. In such case, the compiler typecasts the character implicitly to long type and returns the corresponding ASCII value.

File name: LongExample4.java

Output:

num: 97

Explanation

The Java code provided tries to set a character value ('a') to a long-type variable called num. This is permissible in theory, but it might not work as intended.

The main() method of the LongExample4 class acts as the program's entry point. The variable num is declared and initialized inside the main method using the character literal 'a'.

Unicode values are used in Java to represent characters, and each character has a corresponding numeric Unicode code point.
Java automatically transforms a character value to its equivalent Unicode code point, which is an integer value, when the character is assigned to a long variable. Consequently, 'a' is given to num after being translated to its Unicode code point, which in this case is 97.

Example 5

In this example, long data type holds the minimum and maximum value.

File name: LongExample5.java

Output:

min: -9223372036854775808
max: 9223372036854775807

Explanation

The LongExample5 class's provided Java code shows how to declare and use long variables to represent the lowest and maximum values that are permitted for this data type. Two long variables, max and min, are initialised in the main procedure.

For a long data type, min is the lowest value that can be assigned, represented by -9223372036854775808L, and max is the greatest value that may be assigned, represented by 9223372036854775808L. These values represent the whole set of 64-bit two's complement integers that Java long variables can hold. The code clearly illustrates the extremes of the long data type's range by printing these values using System.out.println().

Example 6

Let's create a method that returns a long type value.

File name: LongExample6.java

Output:

10

Explanation

The LongExample6 class contains the Java code that demonstrates how to declare and use a method called display that takes a long value as input. The method returns the long literal 10L directly using the return statement. The program's entry point, the main method, creates an object of the LongExample6 class and invokes its show method. Then, System.out.println is used to print the returned value, 10L, to the console. This little piece of code clearly illustrates how to create and call a method in a Java class that returns a long number.


Next TopicJava-versions