THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

of UUID values, generally known as types 1, 2, 3, and 4. You can ask a UUID object for its version by using
the version method.


The usual way to create a type 4 (random) UUID is to use the static factory method randomUUID. A type 3
(name-based) UUID can be obtained from the static factory method nameUUIDFromBytes, which takes an
array of bytes representing the name.


You can directly construct an arbitrary form UUID by supplying two long values that represent the upper
and lower halves. Of course, there are no guarantees about uniqueness for such a constructed UUIDit is up to
you to supply appropriate values. The two halves of a UUID object can be retrieved as long values from the
getMostSignificantBits and getLeastSignificantBits methods.


The layout of a UUID is determined by which variant is used to implement it. There are four variants: 0, 2, 6,
and 7. The UUID class always generates variant 2 UUID valuesknown as the Leach-Salz variant. The details
of these variants are not important for this discussion. You can ask a UUID object for its variant by using the
variant method. Two UUID objects are equal only if they have the same variant and 128-bit value.


The toString method returns a string representation of a UUID that can be passed into the static
fromString factory method to get a UUID object.


The remaining methods are applicable only to type 1 (time-based) UUID values, and throw
UnsupportedOperationException if invoked on other types:


public longtimestamp()

Returns the 60-bit timestamp associated with this UUID.

public intclockSequence()

Returns the 14-bit clock sequence value associated with this UUID.

public longnode()

Returns the 48-bit node value associated with this UUID. This value relates to
the host address of the machine on which this UUID was generated.

22.9. Math and StrictMath


The Math class consists of static constants and methods for common mathematical manipulations that use
normal floating-point semantics. The StrictMath class defines the same constants and methods but always
uses specific algorithms. This means that StrictMath methods always return the same values across
different virtual machines, while Math methods are allowed to vary within specified limits. Both classes have
two useful double constants: E approximates e (2.7182818284590452354), and PI approximates π
(3.14159265358979323846). In the following table of the main Math and StrictMath methods, angles are
in radians and all parameters and return values are double unless stated otherwise:


Function Value

sin(a) sine(a)

cos(a) cosine(a)

tan(a) tangent(a)
Free download pdf