Home>Article> How many digits does java long have?

How many digits does java long have?

青灯夜游
青灯夜游 Original
2023-03-06 16:18:50 6477browse

java long is 64 bits. The long data type is a 64-bit signed Java primitive data type. It is used when the calculation result of an integer may exceed the range of the int data type. Its value range is -9223372036854775808 to 9223372036854775807. All integers within the range of long are called integer literals of long type. Integer constants of long type always end with L or lowercase l, for example "long num1 = 0L;".

How many digits does java long have?

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

Java long data type

The long data type is a 64-bit signed Java primitive data type.

Use when the calculation result of an integer may exceed the range of the int data type.

The range is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

All integers within the range of long are called integer literals of type long. Integer constants of type long always end in L or lowercase l.

The following is an example of using an integer literal of type long:

long num1 = 0L; long num2 = 4L; long mum3 = -3; long num4 = 8; long num5 = -1L;

Long integer literal

The integer literal of type long can be used Represented in octal, hexadecimal and binary formats. For example,

long num1; num1 = 25L; // Decimal format num1 = 031L; // Octal format num1 = 0X19L; // Hexadecimal format num1 = 0b11001L; // Binary format

When a long literal is assigned to a variable of type long, the Java compiler checks the value to be assigned and ensures that it is within the scope of the long data type; otherwise a compile-time error is generated.

Note:Assignment from int to long is valid because all values that can be stored in an int variable can be stored in a long variable. However, the opposite is not true. You cannot simply assign the value stored in a long variable to an int variable. There is a possibility of value overflow.

To assign the value of a long variable to an int variable, use "cast" in Java as follows:

num1 = (int)num2;

Java has a class Long which defines two A constant to represent the maximum and minimum values of the long data types Long.MAX_VALUE and Long.MIN_VALUE.

long max = Long.MAX_VALUE; long min = Long.MIN_VALUE;

Extended knowledge: The following code can see how big the 64-bit value is

Long.MAX_VALUE // Prints 9,223,372,036,854,775,807 Long.MIN_VALUE // Prints -9,223,372,036,854,775,808

Perhaps, long can be used to calculate the distance traveled by light in a certain period of time. Light travels approximately 300,000 kilometers in one second. If you write a program to track the distance traveled by light, the int type will be out of range class after 7 seconds, while the long type will be able to calculate about 975 years. Don't believe it? You can take a look at the calculations in this gist (https://gist.github.com/jrg94/820d3f0f482dd19f0170964346381df0).

For more programming-related knowledge, please visit:Programming Teaching! !

The above is the detailed content of How many digits does java long have?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn