Java – int / long,float / double

I understand that "2.5" is automatically double. In order to make it a floating point number, I need to do "2.5f" (or should f be lowercase?), And I should use floating point numbers. For example, if I have a constant that only needs two decimal places (such as "0.08f" of Ontario PST tax), but I'm not sure whether "12" is int or long, but I know that "12L" is long, But "long" L = 12 "and" long l = 12L "seem to compile into the same thing. If I want the maximum non floating point precision, I use long. If I know I don't need to exceed the limit of int, I use int

If there is something wrong, please correct me and answer my question

Solution

As a constant in Java, 12 is an int

Long l = 12 is compiled because it automatically expands to long

Editor: about your comments, there's no problem with automatic extension. Use anything to make your code clearer, but just know what happens when you perform mathematical operations on primitives For example:

int i = 1213;
long l = 112321321L * i;

If you don't explicitly set the first number to long, long will have a very different value because Java treats it as integer math and causes an overflow

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>