Java变量取值范围探讨

探讨Java中的变量取值范围,通过Java自带的函数直接求出其最大值和最小值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class floattest{
public static void main(String[] args) {

byte b1 = Byte.MAX_VALUE;
byte b2 = Byte.MIN_VALUE;
System.out.println("byte的取值范围是:"+b2+" ~ "+b1);

short s1 = Short.MAX_VALUE;
short s2 = Short.MIN_VALUE;
System.out.println("short的取值范围是:"+s2+" ~ "+s1);

int i1 = Integer.MAX_VALUE;
int i2 = Integer.MIN_VALUE;
System.out.println("int的取值范围是:"+i2+" ~ "+i1);

long l1 = Long.MAX_VALUE;
long l2 = Long.MIN_VALUE;
System.out.println("long的取值范围是:"+l2+" ~ "+l1);

float f1 = Float.MAX_VALUE;
float f2 = Float.MIN_VALUE;
System.out.println("float的取值范围是:"+f2+" ~ "+f1);

double d1 = Double.MAX_VALUE;
double d2 = Double.MIN_VALUE;
System.out.println("double的取值范围是:"+d2+" ~ "+d1);
}
}

程序输出结果如下:

1
2
3
4
5
6
byte的取值范围是:-128 ~ 127
short的取值范围是:-32768 ~ 32767
int的取值范围是:-2147483648 ~ 2147483647
long的取值范围是:-9223372036854775808 ~ 9223372036854775807
float的取值范围是:1.4E-45 ~ 3.4028235E38
double的取值范围是:4.9E-324 ~ 1.7976931348623157E308