Java Long Keyword
Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put inside of it.Java's long keyword is used to hold values for 64-bit integers, a Java primitive type. In essence, keywords are terms that are used exclusively in a compiler's context and are therefore reserved.
Declaring astatement, method return value, or variable of data type long integer in Java also implements the term long. Java's Long class turns a primitive value of the long type into an object. Only one field with the long type is present in an object of the long type.
To convert long primitive values into long type objects, a wrapper class is considered. Along with this, the long wrapper class has methods for implementing a long to a String and vice versa as well as additional providing the constants and methods for dealing.
- The long has a value range of 263-1 to -263-263, with -263-1 being the smallest value.
- The long can now be shown as an unsigned 64-bit long with a minimum value of 0 and a maximum value of 264-1.
- The default setting for the long keywrd is 0L.
- 8-byte default size of the long keyword.
- When a larger range of integer values is required, it is employed.
- In the Java programming language, the long class acts as a wrapper class to handle the long primitive type. The range of values for this type is represented by the constants MIN VALUE and MAX VALUE.
- Excepting the value that is followed by l or L, as in 235L, which declares that the value should be considered as a long, all integer literals in the Java programming language are 32-bit int values only.
Syntax for declaring Long:
Long var_ name = value;
Here are some more examples.
long anotherNumber = 34590L;
long octalNumber = 0377;
long hexNumber = 0xffl;
Examples of the Java Long Keyword
Example 1:
This is the program to implement the long Keyword for printing positive and negative values
public class LongEx1
{
public static void main (String...s)
{
long R=5L;
long S=-15L;
System.out.println("R: "+R);
System.out.println("S: "+S);
}
}
Output of the given program is:
R: 5
S: -15
Example 2:
Program to illustration for determining whether a long data type can store decimal values.
public classLongEx2
{
public static void main (String...s)
{
long n=11.5;
System.out.println("n: "+n);
}
}
Output:
error: incompatible types: possible lossy conversion from double to long
Example 3:
public class LongEx3
{
public static void main (String...s)
{
long n=111f;
System.out.println("n: "+n);
}
}
Output:
error: incompatible types: possible lossy conversion from float to long
Example 4:
public class long_Key 1{
public static void main (String s []){
sum (34656,45634);
}
static long sum (long l1, long l2){
//long keyword is used to method as return type
//long keyword is used as parameter
long ans=l1+l2;
System.out.println("The result is :"+ans);
return ans;
}
}
Output:
The result is :80290
Example 5:
Let's look at an illustration to see if the long data type supports char values. When this occurs, the compiler implicitly converts the character to long type and outputs the associated ASCII value.
public class LongEx5
{
public static void main (String...s)
{
long m='b';
System.out.println("m: "+m);
}
}
Output of the program is given by:
m: 98
Example 6:
Program for checking the larger and smaller value by using long data type
public class LongEx 6
{
public static void main (String...s)
{
long low=-9223372036854775808L;
long high=9223372036854775807L;
System.out.println("low: "+low);
System.out.println("high: "+high);
}
}
Output:
low: -9223372036854775808
high: 9223372036854775807
Example 7:
Program to create a method that returns a value of the long type.
public class LongEx 7{
public long disp()
{
return 15L;
}
public static void main (String [] s) {
LongEx7 bj=new LongEx7();
System.out.println(bj. disp ());
}
}
Output:
15