long integer data type
The long data type is a type of integer data type just like int, byte and short. However, long has 64 bits of memory. That’s , it’s the largest of all the integer values.
nanadwumor
- The long data type is an integer.
- long comprises 64 bits of memory. It has the largest memory of all the 4 integer data types.
-
The range of values of a long are between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
RECOMMENDED ARTICLES
Variables and Literals in Java
Variables are used in many situations. For Instance, game applications use variables to collect your name when you sign in; to store and keep track of your number of trials, successes and failures. In Java, a variable is a container used to store...
double data type in Java
The double data type is also a floating point number. It is called double because it has a double-precision decimal number. The double data type is a floating point number. It is double precision data type The range is 4.9406564584124654 x...
The print and println methods in Java
The print() and println() methods are Java's predefined methods used to display text output on the console. These methods are part of the Java API. The print and println methods are used to print string of characters onto the console The print...
The long data type is a type of integer data type just like int, byte and short.
However, long has 64 bits of memory. That’s , it’s the largest of all the integer values.
The range of values of a long are between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Join Other Subscribers on Our YouTube Channel and Don’t Miss a thing!
Declaring a long variable
A long variable is declared just as an int, byte or short variable. Examples include:
long distance;
long height;
Alternatively, you can also declare all long variables in a row. That’s,
long distance, height;
Like an int, the default variable of a long is 0.
When you initialize a long variable, you must append either l or L to it else Java will treat it as an int by default.
Program demonstrates long data type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
//Program demonstrates the long data type public class LongVariables { public static void main(String[] args) { /*distance1 is a long data type. The number 987654321 is treated as an int by default. It is within the int range and the long range at too. Java picks the smallest possible range, which is int. The compiler does not complain because an int value can be stored in a long variable. Note that long has a bigger memory range than int. */ long distance1=987654321; /*distance2 is a long data type. 9876543210 is treated as an int by the compiler. * Unfortunately, 9876543210 is out of range of the int data type. * It has ten digits and that's beyond an int. * The Java compiler will complain that int is out of range. * To correct this, we append l or L to explicitly cast from int * data type to long data type. */ //long distance2 = 9876543210; System.out.println("The distance is " + distance1 + " feet"); } } |
Output
Explanation of Code
distance1 is a long data type. The number 987654321 is treated as an int by default. It is within the int range and the long range at too. Java picks the smallest possible range, which is int.
long distance1=987654321; //987654321 is treated as int
The compiler does not complain because an int value can be stored in a long variable. Note that long has a bigger memory range than int.
long distance2=9876543210;
distance2 is a long data type. 9876543210 is treated as an int by the compiler. Unfortunately, 9876543210 is out of range of the int data type. It has ten digits and that’s beyond an int. The Java compiler will complain that int is out of range. To correct this, we append l or L to explicitly cast from int data type to long data type.
Like all other primitive data types, the print and println methods can print the long data type.
You May Also Like…
Variables and Literals in Java
Variables are used in many situations. For Instance, game applications use variables to collect...
double data type in Java
The double data type is also a floating point number. It is called double because it has a...
The print and println methods in Java
The print() and println() methods are Java's predefined methods used to display text output on the...
0 Comments