In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "why there is f after the number when using float in java". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "why there is f after the number when using float in java".
Catalogue
The difference between Java float and double
1. Float is a single-precision floating point number
2. The decimal declared by default in java is of type double.
3. For programmers,
The problem of adding f, d, L suffixes to the assignment of variables of Java float, double and long
1. Add a suffix description
2. if you don't add a suffix, you won't report an error.
3. Summary
The difference between Java float and double 1. Float is a single-precision floating-point number
Memory allocation is 4 bytes, accounting for 32 bits, and effective decimal places 6-7 places
Double is a double-precision floating-point number with 8 bytes of memory allocated, accounting for 64 bits, and 15 valid decimal places
2. The decimal declared by default in java is of type double.
For example, double dudes 4.0
An error will be reported if you declare: float x = 4.0f or float x = (float) 4.0f.
The f after 4.0f is only to distinguish double and does not represent any numerical meaning.
3. For programmers,
The difference between double and float is that double has high precision, but double consumes twice as much memory as float, and the operation speed of double is slightly slower than float.
Java float, double, long type variable assignment add f, d, L suffix problem 1, add suffix description
We know that when Java assigns variables, float, double, and long data type variables need to be explained by adding f or F, d or D, l or L suffixes respectively after the direct amount of assignment.
Among them, the long type is best suffixed with an uppercase L, because lowercase l is easily confused with the number 1.
For example:
Long lNum = 1234L / float fNum = 1.23f / double dNum = 1.23d
This is the Java syntax, not adding a suffix can easily cause the compiler to report an error, and the readability of the program will become worse.
2. if you don't add a suffix, you won't report an error.
In the Java language, integers are direct quantities (for example, 1, 2, 10, etc.), and the JVM virtual machine defaults to int type data. Therefore, when integers are directly assigned to long, float, or double without adding a suffix, the virtual machine will automatically convert the int type data to the corresponding type and then assign the value. Because the conversion from a short data length to a long data length does not cause data loss, it can be converted automatically by default.
For example:
Long lNum = 5; / / No error is reported, because int automatically converts to long type and does not report error float fNum = 7; / / does not report error, because int automatically converts to float type and does not report error double dNum = 10; / / ditto
Therefore, when you encounter these three types of variables in Java that need to be assigned a direct amount, it is best to add the corresponding suffixes. This will not only prevent the compiler from reporting errors, but also increase the readability of the program.
But in the following case, it is wrong to add a suffix, because the suffix is only to tell the virtual machine what data type the direct number belongs to, and the data type cannot be cast.
Long lNum = 1.2L; / / error. Double type data cannot be directly assigned to long type long lNew = (long) 1.2 / / correct, double type data cast to long type thank you for reading, the above is "why there is f after the number when using float in java", after the study of this article, I believe you have a deeper understanding of why there is f behind the number when using float in java, and the specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.