Java long Keyword22 Feb 2025 | 6 min read 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
Applications of long Keyword
Examples of Java long keywordExample 1Let'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 2Let'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 3Let'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 4Let'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. Example 5In 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 6Let'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 |
Hybrid Inheritance in Java
In Java, inheritance is the most important OOP concept that allows to inherit the properties of a class into another class. In general, it defines an IS-A relationship. By using the inheritance feature, we can derive a new class from an existing one. Java supports the following four types...
7 min read
Implementing Sparse Vector in Java
Sparse vectors constitute an essential data structure in many applications, such as scientific computing, machine learning, and information retrieval. They are especially helpful when working with high-dimensional data, where the majority of the elements are zeros. This article offers a thorough walkthrough of creating a...
5 min read
DoubleBuffer get() methods in Java with Examples
The java.nio.DoubleBuffer has get() function. The DoubleBuffer Class is utilized to read the double at the current position of the buffer and then increment that position. Syntax: public abstract double get() Return Value: The double value at the current position of the buffer is returned by...
3 min read
Designing a Vending Machine in Java
Vending machines have become an integral part of our daily lives, offering a convenient way to access a variety of snacks and beverages. Behind their seemingly simple functionality lies a complex software design that ensures smooth user interactions and inventory management. In this section, we will...
7 min read
Java BLOB
In Java, BLOB and CLOB are the two data types used to store binary and character large objects, respectively. It is different from other data types like float, int, double, etc. Collectively it refers to as LOB (Large Objects). In this section, we will discuss the BLOB...
4 min read
Meta Class Vs. Super Class in Java
Java is an object-oriented programming language that uses a number of ideas to arrange and structure code. In this context, Meta Class and Super Class are two essential ideas. While they each have a part to play in maintaining links across classes, their functions and applications...
5 min read
Java Convert Bytes to Unsigned Bytes
In Java, byte is data type. It is 8-bit signed (+ ive or - ive) values from -128 to 127. The range of unsigned byte is 0 to 255. Note that Java does not provide unsigned byte. If we need to represent a number as unsigned...
3 min read
Ganesha's Pattern in Java
In this section, we will learn about how we can write the code for Lord by using stars or any other special characters. It is one of the most difficult pattern programs to code in Java. We will use the 'for' loop to print the Lord...
2 min read
Advantages of Inner Class in Java
With the help of Java's inner classes, programmers can organize and group their code in a more logical and modular fashion. Inner classes are defined inside other classes, as the name suggests. In this section, we will look at the advantages of using inner classes in...
5 min read
Image Processing in Java: Coloured Image to Grayscale Image Conversion
Image processing is a significant aspect of computer vision and is widely used in various applications such as medical imaging, security, and multimedia. One of the fundamental operations in image processing is converting a colored image to a grayscale image. Grayscale images are simpler and...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India