Professional Documents
Culture Documents
Chapter 04
Chapter 04
False
True
Test2? Test2 true code
False
...
False
True
Last? Last true code
False
\t tab
\b backspace
\\backlash
Figure 4.24 Escape sequences for special characters
The byte, short, and long Types
• byte: one byte = 8 bits, range from -128 to 127
• short: two bytes = 16 bits
– range from -32,768 to 32,767
• int: four bytes = 32 bits
– range from -2,147,483,648 to 2,147,483,647
• long: eight bytes = 64 bits
– range 9,223,372,036,854,808 to 9,223,372,036,854,807
• int is the default for integer constant
• Integer constants can be made long by adding the
suffix l or L
The float Type
• float: four bytes = 32 bits
– Exponent can range from -45 to 48
– Min ~ -3.4E+38 with seven significant digits
– Max ~ 3.4E+38 with seven significant digits
• double: eight bytes = 64 bits
– Exponent can range from -324 to 308
– Min ~ -1.7E+308 with 16 significant digits
– Max ~ 1.7E+308 with 16 significant digits
• double is the default for real constants
• Make a number float by adding an f or F suffix
float good = 4.25f; // valid
float ok = (float)4.25; // valid
float bad = 4.25; // invalid
The Math Class
• In java.lang package
• Two static constants: E (base of natural log) and
PI (ratio of circumference of a circle to its
diameter)
• General functions
abs - absolute value
ceil - ceiling, smallest integer >= argument
floor - flooring, greatest integer <= argument
round - rounding, integer closest to argument
max - maximum of the two arguments
min - minimum of the two arguments
The Math Class
• Simple mathematical operations
sqrt(double a) - Return the square root of the argument
pow(double a, double b) - Returns of value of the first
argument raised to the power of the second argument
log(double a) - Returns the natural logarithm (base e) of
a double value
exp(double a) - Returns the exponential number e (i.e.,
2.718...) raised to the power of a double value
• Random number
random() - Returns a double value with a positive sign,
greater than or equal to 0.0 and less than 1.0
The Math Class
• Trigonometric functions
sin, cos, tan - sine, cosine, and tangent
asin, acos, atan- arc-sine, arc-cosine, and arc-tangent
atan2(double a, double b) - Returns the theta
component in the polar coordinates (r, theta)
corresponding to the point (b, a) in Cartesian
coordinates
• Degree/Radian conversion
toDegrees(double angrad) - Converts an angle in
radians to the equivalent angle measured in degrees
toRadians(double angdeg) - Converts an angle in
degrees to the equivalent angle measured in radians
The Math Class
• Library.java
• Toss.java
• CoinToss.java
16
(grains of rice)
Reward
4
2
1
1 2 4 8 12 16
Days