Java 基础类型范围
Java 基础类型范围
import java.util.Set; import java.util.TreeSet; public class FoundationalTypes { private static int index=0; private static Set set=new TreeSet(); /** * The infomation of types will be printed * @param className * @throws InstantiationException * @throws IllegalAccessException * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchFieldException */ private void printInfo(String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SecurityException, NoSuchFieldException{ Class c=Class.forName(className); String MIN_VALUE=""; String MAX_VALUE=""; if("java.lang.Character".equals(className)){ MIN_VALUE=(int)(java.lang.Character)c.getField("MIN_VALUE").get(c)+""; MAX_VALUE=(int)(java.lang.Character)c.getField("MAX_VALUE").get(c)+""; }else{ MIN_VALUE=(c.getField("MIN_VALUE").get(c)).toString(); MAX_VALUE=(c.getField("MAX_VALUE").get(c)).toString(); } System.out.println((++index)+"、基本类型:"+c.getField("TYPE").get(c)+" 二进制位数:"+c.getField("SIZE").get(c)); System.out.println((++index)+"、包装类:"+c.getName()); System.out.println((++index)+"、最小值(MIN_VALUE):"+MIN_VALUE); System.out.println((++index)+"、最大值(MAX_VALUE):"+MAX_VALUE); System.out.println(); int k= Integer.valueOf(c.getField("SIZE").get(c).toString()); set.add((k<10?"0"+k:k)+":"+c.getName()); } /** * running * @param args */ public static void main(String[] args) { try { String[] types={ "java.lang.Long" ,"java.lang.Double" ,"java.lang.Integer" ,"java.lang.Float" ,"java.lang.Character" ,"java.lang.Short" ,"java.lang.Byte" // ,"java.lang.Boolean" }; for(String name : types){ new FoundationalTypes().printInfo(name); } StringBuffer starts=new StringBuffer(" \t\t*"); System.out.println("08:java.lang.Boolean"+starts); for(Object obj:set){ starts.append("*"); System.out.println(obj.toString()+starts); } } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行结果为:
1、基本类型:long 二进制位数:64
2、包装类:java.lang.Long
3、最小值(MIN_VALUE):-9223372036854775808
4、最大值(MAX_VALUE):9223372036854775807
5、基本类型:double 二进制位数:64
6、包装类:java.lang.Double
7、最小值(MIN_VALUE):4.9E-324
8、最大值(MAX_VALUE):1.7976931348623157E308
9、基本类型:int 二进制位数:32
10、包装类:java.lang.Integer
11、最小值(MIN_VALUE):-2147483648
12、最大值(MAX_VALUE):2147483647
13、基本类型:float 二进制位数:32
14、包装类:java.lang.Float
15、最小值(MIN_VALUE):1.4E-45
16、最大值(MAX_VALUE):3.4028235E38
17、基本类型:char 二进制位数:16
18、包装类:java.lang.Character
19、最小值(MIN_VALUE):0
20、最大值(MAX_VALUE):65535
21、基本类型:short 二进制位数:16
22、包装类:java.lang.Short
23、最小值(MIN_VALUE):-32768
24、最大值(MAX_VALUE):32767
25、基本类型:byte 二进制位数:8
26、包装类:java.lang.Byte
27、最小值(MIN_VALUE):-128
28、最大值(MAX_VALUE):127
08:java.lang.Boolean *
08:java.lang.Byte **
16:java.lang.Character ***
16:java.lang.Short ****
32:java.lang.Float *****
32:java.lang.Integer ******
64:java.lang.Double *******
64:java.lang.Long ********