Java long KeywordThe 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 |
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