Home  >  Article  >  Java  >  How to define Long type in Java

How to define Long type in Java

王林
王林forward
2023-05-02 16:40:143673browse

Java defines Long data type

Long lg=10L;

Just add an L after the defined integer type;

It is the same as defining float data type

Float ft=5.20F;

Java definition Why do we need to add L and F to long and float?

Why do we need to add L when defining long type variables?

  • Basic data type int, occupies 4 bytes, the value range is -231 ~ 231-1, converted to decimal is -2147483648 ~ 2147483647

  • Basic data type long, occupies 8 bytes, the value range is -263 ~ 263-1, converted to decimal is -9223372036854775808 ~ 9223372036854775807

We know that in In Java, the default data type for integers is int. When we assign an integer to a variable of any type, the integer defaults to type int.

If this number is less than the maximum value of int, you can directly assign a value to long, because the value range of int is smaller than the long type and can be automatically converted.

If this number is greater than the maximum value of int, automatic conversion cannot be performed at this time. We need to add L after the number to perform forced conversion, otherwise an error will be reported.

How to define Long type in Java

#Why do you need to add F when defining a float type variable?

  • Basic data typefloat, occupies 4 bytes, the value range is -3.40E 38 ~ 3.40E 38

  • Basic data typedouble, occupies 8 bytes, the value range is -1.79E 308 ~ 1.79E 308

The default data type of floating point numbers in Java is double. When we assign a floating point number to a variable of any type, the floating point number defaults to double type.

If we assign an integer to float, because the value range of float is greater than int, it will be converted automatically

If we assign a floating point number to float, because the value range of float is smaller than double , then you need to add F at the end for forced transfer

How to define Long type in Java

The above is the detailed content of How to define Long type in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete