ch02 - Introduction To Java Applications Input-Output and Operators
ch02 - Introduction To Java Applications Input-Output and Operators
Introduction to
Java Applications;
Input/Output and
Operators
From the textbook
Java How to Program 11/e
• Integers
§ Whole numbers, like –22, 7, 0 and 1024)
• Programs remember numbers and other data in the computer’s
memory and access that data through program elements called
variables.
• The program of Fig. 2.7 demonstrates these concepts.
Fig. 2.7: Addition program that inputs two number then displays their sum
5 import Declarations
• Prompt
§ Output statement that directs the user to take a specific action.
• Class System
§ Part of package java.lang.
§ Class System is not imported with an import declaration at the beginning
of the program.
• Variables
§ Every variable has a name, a type, a size (in bytes) and a value.
§ When a new value is placed into a variable, the new value replaces the
previous value (if any). The previous value is lost, so this process is said to be
destructive.
§ When a value is read from a memory location, the process is nondestructive.
7 Arithmetic
• Arithmetic operators are summarized in Fig. 2.11.
• The asterisk (*) indicates multiplication
• The percent sign (%) is the remainder operator
• The arithmetic operators are binary operators because they each operate on
two operands.
• Integer division yields an integer quotient.
§ Any fractional part in integer division is simply truncated (i.e., discarded) —no
rounding occurs.
• The remainder operator, %, yields the remainder after division.
7 Arithmetic (Cont.)
• Arithmetic expressions in Java must be written in straight-line form to
facilitate entering programs into the computer.
• Expressions such as “a divided by b” must be written as a / b, so that
all constants, variables and operators appear in a straight line.
• Parentheses are used to group terms in expressions in the same manner
as in algebraic expressions.
• If an expression contains nested parentheses, the expression in the
innermost set of parentheses is evaluated first.
7 Arithmetic (Cont.)
• Rules of operator precedence
§ Multiplication, division and remainder operations are applied first.
§ If an expression contains several such operations, they are applied from
left to right.
§ Multiplication, division and remainder operators have the same level of
precedence.
§ Addition and subtraction operations are applied next.
§ If an expression contains several such operations, the operators are
applied from left to right.
§ Addition and subtraction operators have the same level of precedence.
• When we say that operators are applied from left to right, we are
referring to their associativity.
• Some operators associate from right to left.
• Complete precedence chart is included in Appendix A.
8 Decision Making: Equality and
Relational Operators
• Condition
§ An expression that can be true or false.
• if selection statement
§ Allows a program to make a decision based on a condition’s value.
• Equality operators (== and !=)
• Relational operators (>, <, >= and <=)
• Both equality operators have the same level of precedence,
which is lower than that of the relational operators.
• The equality operators associate from left to right.
• The relational operators all have the same level of
precedence and also associate from left to right.
8 Decision Making: Equality and
Relational Operators (Cont.)
• An if statement always begins with keyword if, followed by a
condition in parentheses.
§ Expects one statement in its body, but may contain multiple statements if they
are enclosed in a set of braces ({}).
§ The indentation of the body statement is not required, but it improves the
program’s readability by emphasizing that statements are part of the body.
• Note that there is no semicolon (;) at the end of the first line of each
if statement.
§ Such a semicolon would result in a logic error at execution time.
§ Treated as the empty statement—semicolon by itself.
8 Decision Making: Precedence and
associativity of operators
End of
Chapter 2