Java如何获取基本数据类型的最小值和最大值?
为了得到一个基本数据类型如的最小值或最大值byte,short,int,long,float和double我们可以使用提供用于它们中的每包装类,包装类是Byte,Short,Integer,Long,Float和Double其位于java.lang包。
package org.nhooo.example.lang; public class MinMaxExample { public static void main(String[] args) { System.out.println("Byte.MIN = " + Byte.MIN_VALUE); System.out.println("Byte.MAX = " + Byte.MAX_VALUE); System.out.println("Short.MIN = " + Short.MIN_VALUE); System.out.println("Short.MAX = " + Short.MAX_VALUE); System.out.println("Integer.MIN = " + Integer.MIN_VALUE); System.out.println("Integer.MAX = " + Integer.MAX_VALUE); System.out.println("Long.MIN = " + Long.MIN_VALUE); System.out.println("Long.MAX = " + Long.MAX_VALUE); System.out.println("Float.MIN = " + Float.MIN_VALUE); System.out.println("Float.MAX = " + Float.MAX_VALUE); System.out.println("Double.MIN = " + Double.MIN_VALUE); System.out.println("Double.MAX = " + Double.MAX_VALUE); } }
上面的代码结果显示每种数据类型的最小值和最大值。
Byte.MIN = -128 Byte.MAX = 127 Short.MIN = -32768 Short.MAX = 32767 Integer.MIN = -2147483648 Integer.MAX = 2147483647 Long.MIN = -9223372036854775808 Long.MAX = 9223372036854775807 Float.MIN = 1.4E-45 Float.MAX = 3.4028235E38 Double.MIN = 4.9E-324 Double.MAX = 1.7976931348623157E308