Java Reference
In-Depth Information
Java programs work by having things called objects perform actions. The actions
performed by an object are called methods. System.out is an object used for sending
output to the screen; println is the method (that is, the action) that this object per-
forms. The action is to send what is in parentheses to the screen. When an object per-
forms an action using a method, that is called invoking the method (or calling the
method). In a Java program, you write such a method invocation by writing the object
followed by a dot (period), followed by the method name, and some parentheses that
may or may not have something inside them. The thing (or things) inside the paren-
theses is called an argument(s) and provides information needed by the method to
carry out its action. In each of these two lines and the similar line that follows them,
the method is println . The method println writes something to the screen, and the
argument (a string in quotes) tells it what it should write.
Invoking a method is also sometimes called sending a message to the object. With
this view a message is sent to the object (by invoking a method) and in response the
object performs some action (namely the action taken by the method invoked). We
seldom use the terminology sending a message, but it is standard terminology used by
some programmers and authors.
Variable declarations in Java are similar to what they are in other programming lan-
guages. The following line from Display 1.1 declares the variable answer :
invoking
dot
argument
sending a
message
variable int
int answer;
The type int is one of the Java types for integers (whole numbers). So, this line says
that answer is a variable that can hold a single integer (whole number).
The following line is the only real computing done by this first program:
equal sign
answer = 2 + 2;
In Java, the equal sign is used as the assignment operator , which is an instruction
to set the value of the variable on the left-hand side of the equal sign. In the preceding
program line, the equal sign does not mean that answer is equal to 2 + 2 . Instead, the
equal sign is an instruction to the computer to make answer equal to 2 + 2 .
The last program action is
assignment
operator
System.out.println("2 plus 2 is " + answer);
This is an output statement of the same kind as we discussed earlier, but there is some-
thing new in it. Note that the string "2 plus 2 is " is followed by a plus sign and the
variable answer . In this case the plus sign is an operator to concatenate (connect) two
strings. However, the variable answer is not a string. If one of the two operands to + is
a string, Java will convert the other operand, such as the value of answer , to a string. In
this program, answer has the value 4 , so answer is converted to the string "4" and then
concatenated to the string "2 plus 2 is " , so the output statement under discussion
is equivalent to
System.out.println("2 plus 2 is 4");
Search WWH ::




Custom Search