Variables and Assignment Primitive Data Types Expressions Data Conversion Character Strings
Variables and Assignment Primitive Data Types Expressions Data Conversion Character Strings
int total;
int count, temp, result;
int sum = 0;
int base = 32, max = 149;
total = 55;
uppercase letters A, B, C, …
lowercase letters a, b, c, …
punctuation period, semi-colon, …
digits 0, 1, 2, …
special symbols &, |, \, …
control characters carriage return, tab, ...
Boolean
• A boolean value represents a true or false
condition
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
8 / 12 equals 0
14 % 3 equals 2
8 % 12 equals 8
Operator Precedence
• Operators can be combined into complex
expressions
result = total + count / max - offset;
a + b + c + d + e a + b * c - d / e
1 2 3 4 3 1 4 2
a / (b + c) - d % e
2 1 4 3
a / (b * (c + (d - e)))
4 3 2 1
Expression Trees
• The evaluation of a particular expression can be
shown using an expression tree
• The operators lower in the tree have higher
precedence for that expression
+
a + (b – c) / d
a /
- d
b c
Assignment Revisited
• The assignment operator has a lower precedence
than the arithmetic operators
count = count + 1;
count++
• prefix form:
++count
is equivalent to
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y
Assignment Operators
• The right hand side of an assignment operator can
be a complex expression
• Therefore
result /= (total-MIN) % num;
is equivalent to
result = result / ((total-MIN) % num);
Assignment Operators
• The behavior of some assignment operators
depends on the types of the operands
money = dollars
• Examples:
"This is a string literal."
"123 Main Street"
"X"
• Every character string is an object in Java, defined
by the String class
• Every string literal represents a String object
The println Method
• In the Myprogram program from Chapter 1, we
invoked the println method to print a character
string
• The System.out object represents a destination
(the monitor screen) to which we can send output
object method
information provided to the method
name
(parameters)
The print Method
• The System.out object provides another service
as well
\b backspace
\t tab
\n newline
\r carriage return
\" double quote
\' single quote
\\ backslash
Countdown.java
public class Countdown
{
//-----------------------------------------------------------------
// Prints two lines of output representing a rocket countdown.
//-----------------------------------------------------------------
public static void main (String[] args)
{
System.out.print ("Three... ");
System.out.print ("Two... ");
System.out.print ("One... ");
System.out.print ("Zero... ");
System.out.println ();
// A string can contain numeric digits
System.out.println ("Letters in the Hawaiian alphabet: 12");