Numbers and Math
Java provides the
byte
, short
,
int
, long
,
float
, and double
primitive types for representing
numbers. The java.lang
package includes the corresponding
Byte
, Short
,
Integer
, Long
,
Float
, and Double
classes, each
of which is a subclass of Number
. These classes
can be useful as object wrappers around their primitive types, and
they also define some useful constants:
// Integral range constants: Integer, Long, and Character also define these Byte.MIN_VALUE // The smallest (most negative) byte value Byte.MAX_VALUE // The largest byte value Short.MIN_VALUE // The most negative short value Short.MAX_VALUE // The largest short value // Floating-point range constants: Double also defines these Float.MIN_VALUE // Smallest (closest to zero) positive float value Float.MAX_VALUE // Largest positive float value // Other useful constants Math.PI // 3.14159265358979323846 Math.E // 2.7182818284590452354
Mathematical Functions
The
Math
class
defines a number of methods that provide trigonometric, logarithmic,
exponential, and rounding operations, among others. This class is
primarily useful with floating-point values. For the trigonometric
functions, angles are expressed in radians. The logarithm and
exponentiation functions are base e, not base
10. Here are some examples:
double d = Math.toRadians(27); // Convert 27 degrees to radians d = Math.cos(d); // Take the cosine d = Math.sqrt(d); // Take the square root d = Math.log(d); // Take the natural logarithm d = ...
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.