Java Long Type

Java Long Type


Long Type In Java
 
NOTE -
We can implicitly typecast the integer value to the long type of variable, but we can not implicitly convert the long type of variable into an integer type of variable if we make an attempt to do such a thing the compiler will through an error.
 
Long is a keyword used to declare long data type variables by putting long keywords before the identifier or the variable name.
The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. 
This is used when int is not large enough to store the value.
Note that you should end the value with an "L".
Otherwise,  the program will through error.
In other words - 
long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value. 
The range of a long is quite large. 
This makes it useful when big, whole numbers are needed. 
Now we will see Example - 
Example -
For example, there is a program that computes the number of miles that light will travel in a specified number of days.
// Web designing house
// online promotion house
 
//Compute distance light travels using long variables.
class Light {
public static void main(String args[]) {
int lightspeed;
long days;
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days);
System.out.print(" days light will travel about ");
System.out.println(distance + " miles.");
}
}

 
Output - 
A sample output would be - 
In 1000 days light will travel about 16070400000000 miles