Java Reference
In-Depth Information
There are many things you can do with expressions. One of the simplest things
you can do is to print the value of an expression using a println statement. For
example, if you say:
System.out.println(42);
System.out.println(2 + 2);
you will get the following two lines of output:
42
4
Notice that for the second println , the computer evaluates the expression
(adding 2 and 2) and prints the result (in this case, 4).
You will see many different operators as you progress through this topic, all of
which can be used to form expressions. Expressions can be arbitrarily complex, with
as many operators as you like. For that reason, when we tell you, “An expression can
be used here,” we mean that you can use arbitrary expressions that include complex
expressions as well as simple values.
Literals
The simplest expressions refer to values directly using what are known as literals. An
integer literal (considered to be of type int ) is a sequence of digits with or without a
leading sign:
3 482 29434 0 92348 +9812
A floating-point literal (considered to be of type double ) includes a decimal point:
298.4 0.284 207. .2843 -17.452 -.98
Notice that 207 . is considered a double even though it coincides with an integer,
because of the decimal point. Literals of type double can also be expressed in scien-
tific notation (a number followed by e followed by an integer):
2.3e4 1e-5 3.84e92 2.458e12
The first of these numbers represents 2.3 times 10 to the 4th power, which equals
23,000. Even though this value happens to coincide with an integer, it is considered
to be of type double because it is expressed in scientific notation. The second num-
ber represents 1 times 10 to the -5th power, which is equal to 0.00001. The third
number represents 3.84 times 10 to the 92nd power. The fourth number represents
2.458 times 10 to the 12th power.
We have seen that textual information can be stored in literal strings that store
a sequence of characters. In later chapters we will explore how to process a string
 
Search WWH ::




Custom Search