平时对各种数据的取值范围没有在意过,但是真正要讲的时候总是很模糊,然后今天查了些资料然后整理一下,留着以后查看。
Java中每种基本类型所占储存空间的大小并不像其他大多数语言那样随着机器硬件架构的变化而变化。这也是Java程序比其他大多数语言编写的程序更具可移植性的原因之一。各种基本类型的大小定义如下:
char 16bit byte 8bit short 16bit int 32bit long 64bit float 32bit double 64bit(摘自《JAVA编程思想fourth edition》)
public class Demo{ public static void main(String[] args) { //用在钱上不一定够,20忆左右 System.out.println("\nint型10位数长度"); System.out.println("int(max) = " + Integer.MAX_VALUE); System.out.println("int(max) = " + Integer.toHexString(Integer.MAX_VALUE)); System.out.println("int(min) = " + Integer.MIN_VALUE); System.out.println("int(min) = " + Integer.toHexString(Integer.MIN_VALUE)); //可以用在钱上面 System.out.println("\nlong型19位数长度"); System.out.println("long(max) = " + Long.MAX_VALUE); System.out.println("long(max) = " + Long.toHexString(Long.MAX_VALUE)); System.out.println("long(min) = " + Long.MIN_VALUE); System.out.println("long(min) = " + Long.toHexString(Long.MIN_VALUE)); //主要用来做简单数学精确运算使用 System.out.println("\nfloat型38位数长度"); System.out.println("float(max) = " + Float.MAX_VALUE); System.out.println("float(min) = " + Float.MIN_VALUE); //主要用来做复杂运算和天文运算 System.out.println("\ndouble型308位数长度"); System.out.println("double(max) = " + Double.MAX_VALUE); System.out.println("double(min) = " + Double.MIN_VALUE); } }
#include <stdio.h> #include <float.h> //float double #include <limits.h> //char short int long longlong int main() { puts("short型的最大值和最小值:\n"); printf("short(max) = %hd\n", SHRT_MAX); printf("short(max) = %#hx\n", SHRT_MAX); printf("short(min) = %hd\n", SHRT_MIN); printf("short(min) = %#hx\n", SHRT_MIN); puts("\nint型的最大值和最小值:\n"); printf("int(max) = %d\n", INT_MAX); printf("int(max) = %#x\n", INT_MAX); printf("int(min) = %d\n", INT_MIN); printf("int(min) = %#x\n", INT_MIN); puts("\nlong型的最大值和最小值:\n"); printf("long(max) = %ld\n", LONG_MAX); printf("long(max) = %#lx\n", LONG_MAX); printf("long(min) = %ld\n", LONG_MIN); printf("long(min) = %#lx\n", LONG_MIN); puts("\nlonglong型的最大值和最小值:\n"); printf("long long(max) = %lld\n", LLONG_MAX); printf("long long(max) = %#llx\n", LLONG_MAX); printf("long long(min) = %lld\n", LLONG_MIN); printf("long long(min) = %#llx\n", LLONG_MIN); puts("\nfloat型的最大值和最小值:\n"); printf("float(max) = %f\n", FLT_MAX); printf("float(max) = %e\n", FLT_MAX); printf("float(min) = %f\n", FLT_MIN); printf("float(min) = %e\n", FLT_MIN); puts("\ndouble型的最大值和最小值:\n"); printf("double(max) = %lf\n", DBL_MAX); printf("double(max) = %e\n", DBL_MAX); printf("double(min) = %lf\n", DBL_MIN); printf("double(min) = %e\n", DBL_MIN); return 0; }