Documentation

The Java™ Tutorials
Hide TOC
Summary of Numbers数字的小结
Trail: Learning the Java Language
Lesson: Numbers and Strings
Section: Numbers

Summary of Numbers数字的小结

You use one of the wrapper classes – Byte, Double, Float, Integer, Long, or Short – to wrap a number of primitive type in an object.您可以使用其中一个包装类–ByteDoubleFloatIntegerLongShort–在对象中封装多个基元类型。The Java compiler automatically wraps (boxes) primitives for you when necessary and unboxes them, again when necessary.Java编译器会在必要时自动为您包装(装箱)原语,并在必要时再次拆箱。

The Number classes include constants and useful class methods.Number类包括常量和有用的类方法。The MIN_VALUE and MAX_VALUE constants contain the smallest and largest values that can be contained by an object of that type.常量MIN_VALUEMAX_VALUE包含该类型对象可以包含的最小值和最大值。The byteValue, shortValue, and similar methods convert one numeric type to another.byteValueshortValue和类似的方法将一种数字类型转换为另一种。The valueOf method converts a string to a number, and the toString method converts a number to a string.valueOf方法将字符串转换为数字,toString方法将数字转换为字符串。

To format a string containing numbers for output, you can use the printf() or format() methods in the PrintStream class.要格式化包含输出数字的字符串,可以使用PrintStream类中的printf()format()方法。Alternatively, you can use the NumberFormat class to customize numerical formats using patterns.或者,您可以使用NumberFormat类使用模式自定义数字格式。

The Math class contains a variety of class methods for performing mathematical functions, including exponential, logarithmic, and trigonometric methods.Math类包含用于执行数学函数的各种类方法,包括指数法、对数法和三角法。Math also includes basic arithmetic functions, such as absolute value and rounding, and a method, random(), for generating random numbers.Math还包括基本的算术函数,如绝对值和舍入,以及生成随机数的方法random()


Previous page: Beyond Basic Arithmetic
Next page: Questions and Exercises: Numbers