Java Reference
In-Depth Information
Note
The float and double types are used to represent numbers with a decimal point.
Why are they called floating-point numbers ? These numbers are stored in scientific nota-
tion internally. When a number such as 50.534 is converted into scientific notation,
such as 5.0534E+1 , its decimal point is moved (i.e., floated) to a new position.
why called floating-point?
Note
To improve readability, Java allows you to use underscores between two digits in a
number literal. For example, the following literals are correct.
long ssn = 232_45_4519 ;
long creditCardNumber = 2324_4545_4519_3415L ;
However, 45_ or _45 is incorrect. The underscore must be placed between two digits.
underscores in numbers
2.18
How many accurate digits are stored in a float or double type variable?
Check
2.19
Point
Which of the following are correct literals for floating-point numbers?
12.3 , 12.3e+2 , 23.4e-2 , -334.4 , 20.5 , 39F , 40D
2.20
Which of the following are the same as 52.534 ?
5.2534e+1 , 0.52534e+2 , 525.34e-1 , 5.2534e+0
2.21
Which of the following are correct literals?
5_2534e+1 , _2534 , 5_2 , 5_
2.11 Evaluating Expressions and Operator Precedence
Java expressions are evaluated in the same way as arithmetic expressions.
Key
Point
Writing a numeric expression in Java involves a straightforward translation of an arithmetic
expression using Java operators. For example, the arithmetic expression
10( y
-
5)( a
+
b
+
c )
3
+
4 x
4
x
9
+
x
-
+
9
¢
+
x
y
5
can be translated into a Java expression as:
( 3 + 4 * x) / 5 - 10 * (y - 5 ) * (a + b + c) / x +
9 * ( 4 / x + ( 9 + x) / y)
Though Java has its own way to evaluate an expression behind the scene, the result of
a Java expression and its corresponding arithmetic expression is the same. Therefore, you
can safely apply the arithmetic rule for evaluating a Java expression. Operators contained
within pairs of parentheses are evaluated first. Parentheses can be nested, in which case the
expression in the inner parentheses is evaluated first. When more than one operator is used
in an expression, the following operator precedence rule is used to determine the order of
evaluation.
evaluating an expression
operator precedence rule
Multiplication, division, and remainder operators are applied first. If an expression
contains several multiplication, division, and remainder operators, they are applied
from left to right.
Addition and subtraction operators are applied last. If an expression contains several
addition and subtraction operators, they are applied from left to right.
 
 
 
Search WWH ::




Custom Search