Java long
Created By: chatGPT
In Java, the
long
data type is a 64-bit signed integer. It can store a wider range of values compared to the int
data type, which is only 32 bits. This is particularly useful when you need to work with very large numbers. The range of a long
is from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.long myLongValue = 9223372036854775807L;
When defining a
long
variable, you should always include the suffix 'L' or 'l' at the end of the number to indicate that it is a long
literal, as shown in the example below.long population = 7800000000L;
You can perform various mathematical operations on
long
values just like you would with int
values. However, be careful with operations that could lead to overflow, especially when dealing with large numbers.long sum = myLongValue + population;
If you want to convert an
int
to a long
, Java will handle it implicitly, but you can also perform an explicit cast if needed. Here is an example:int myIntValue = 100;
long convertedLong = (long) myIntValue;
You can also declare an array of long integers. Here's a quick example of how to do that and iterate through the array.
long[] longArray = {1L, 2L, 3L, 4L};
for (long num : longArray) {
System.out.println(num);
}