What is the range of int,float,long,double values in Java?
Wrote public class Testoutofbound {
public static void Main (string[] args) {
System.out.println (integer.max_value-(-integer.max_value)); Memory overflow
System.out.println (Integer.max_value); 2 of 31 square -1,10 digits, positive of about 2 billion, used on the money is not necessarily enough
System.out.println (Integer.min_value); Negative 2 of the 31-time Square
System.out.println (Long.max_value); 2 of 64 square -1,19 digits, very big, can rest assured use on the money
System.out.println (Long.min_value); Negative 2 of the 64-time Square
System.out.println (Float.max_value); 2 of 128 square-1, 38 digits, more than a long, this is mainly used to do simple mathematical precision operation using
System.out.println (Float.min_value); 2-149-Time Square
System.out.println (Double.max_value); 2 of 1024 square-1, 308 digits, is the float Number 10 times times, mainly used to do complex operations and astronomical operations
System.out.println (Double.min_value); 2-1074-Time Square
}
}
The results of the operation are as follows:
-2
2147483647
-2147483648
9223372036854775807
-9223372036854775808
3.4028235E38
1.4E-45
1.7976931348623157E308
4.9E-324
int and long are very well-behaved according to 2 of the n-th side of the argument, int is 32 bits, long is 64 bits, only float and double like two naughty children can be elusive. The representations of float and double are not the same as int and long, they are based on the IEEE 754 standard, which can be understood as follows:
(1) The two are still 32-bit, like int, the minimum value is only their accuracy, is positive, which is to be noted. If you want to get their negative maximum, add a symbol before their maximum value, such as-float.max_value, Float can represent the largest negative.
(2) Float from left to right, the first bit is the sign bit, 2-9 a total of 8 bits represents the integer digit, 2 8-1 is equal to 128, the latter 23 bits is the decimal, so the maximum value is 2^128-1;
(3) Double from left to right, the first bit is the sign bit, 2-12 is a total of 11 bits represents the integer digits, 2 11-1 times equals 1024. The remaining 20 bits represent decimals, so the maximum value is 2^1024-1.
(4) As the above code, when the positive maximum minus the positive maximum, resulting in a memory leak. The result of the overflow is not correct.
Summary: the storage of int and long,float and double are treated separately. To get to the actual maximum minimum value, only need to take the maximum value on the line.
Int,float,long,double range of values in Java, memory leaks