We’re going to begin our journey in the world
of programming by exploring how to get Java to display information to the
screen. Before you get started, make sure
that you have successfully installed DrJava as outlined in the installation
guide (download pdf). Output in Java is handled by the System.out.println statement,
which is followed by a set of parentheses containing what is intended to be
displayed. To make it easier to
discern Java statements in the text, the Courier New font will be used. The output of Java programs or statements
is boldfaced, and key terms and concepts are underlined. The general form of the output statement is
the following: System.out.println(the
information to display); The simplest thing to display is a numeric
value, which can either be an integer or a decimal number. Each statement is followed by a set of slashes
‘//’ and a short explanation. In Java,
whatever follows ‘//’ on a line is treated as a comment. Here are some examples: System.out.println(10); // this will output 10 System.out.println(-3.4); // this will output -3.4 To execute a single Java statement such as System.out.println (from this point on we will refer to it simply as a println statement), you can use the Interactions Pane in DrJava as shown in
the installation guide. There’s an
important thing to note here: Java statements are case sensitive, so typing system.out.println(10); would
have caused an error (the lower case ‘s’).
In programming terms, a numerical value is referred to as a literal,
so 10 is an integer literal. If the data
we’re trying to print out is not necessarily numeric (or we don’t want Java
to treat it as a number), we would place it between a set of double quotes as
follows: System.out.println(“This
is Programming I”); A message such as the one in the println statement is a String
literal, where a String is simply a sequence of characters enclosed in
double quotes. Beyond displaying numeric values and Strings,
we can use the println statement to evaluate
arithmetic expressions and display their result. Arithmetic expressions in Java share many
similarities with the ones you’re familiar with from grade school. Addition, subtraction, multiplication and
division correspond to the standard operators +, -, *, and /. Here’s an example: System.out.println(3
+ 5); // this will output 8 System.out.println(3 -
-3); // subtracting -3 will yield 6 Multiplication and subtraction work as
expected, while the division of whole numbers yields a whole number: the
result of the division with the decimals removed. Here are some examples: System.out.println(5
/ 2); // this will output 2 System.out.println(1
/ 2); // this will output 0 This is only the case of both operands are
integers. If at least one of them is a
decimal, the result will be a decimal.
An example: System.out.println(2.0
/ 5); // This will output 0.4 System.out.println(2.0 /
5.0); // This will also output 0.4 The reason that the division of integers is
handled differently than real number division is due to the fact that the
operations are handled by two separate parts of the CPU. Another useful arithmetic operator is the
remainder operator, or modulus.
This is designated by the percent sign, and represents the remainder
of the integer division of two operands.
Here are some examples: System.out.println(5
% 2); // this will output 1 System.out.println(10
% 2); // this will output 0 System.out.println(7
% 4); // this will output 3 System.out.println(2
% 5); // this will output 2 An easy way to convince yourself of the result
is to spell out the integer division corresponding to a modulus
operation. Here’s how: 9 % 4 =
1 <=> 9 = 4 * 2 + 1
The arrows point to corresponding elements in
the expressions, while the number 2 on the right is the result of integer
division (i.e. 9/4
= 2). Here’s another example: 3 % 7 =
3 <=> 3 = 7 * 0 + 3 You should now be trying a couple of these to
make sure you got the hang of it. An arithmetic operation can contain more than
two operands. Here’s an example: System.out.println(3
+ 2 + 5); // this will output 10 As we’d expect, Java applies arithmetic and
other operators in an expression according to their order of precedence. The expression System.out.println(3
+ 2 * 5); will produce 13 for output, not 25
since multiplication has higher precedence than addition. Although seemingly straightforward, when
combined with operands of different types some peculiar things can
emerge. For instance, the statement System.out.println(1
/ 2 * 4.0); will output 0.0 since the operation 1 /
2 is evaluated first producing 0, which is then multiplied by 4.0 to yield
0.0. The mathematically equivalent
expression in the statement System.out.println(4.0 * 1 / 2); on the other hand outputs 2.0 since it’s the
multiplication that’s applied first.
We will look at operator precedence in more detail in the next unit,
but as a rule of thumb, when unsure use parentheses to force an operation to
be applied first. The statement System.out.println((3
+ 2) * 5); will output
25. |