Data and Expressions in Java
Data and Expressions in Java
Java Foundations:
Introduction to Programming and Data Structures
by John Lewis,
Peter DePasquale and Joseph Chase
object method
information provided to the method
name
(parameters)
System.out.println ();
(more…)
\b backspace
\t tab
\n newline
\r carriage return
\" double quote
\' single quote
\\ backslash
int total;
int count, temp, result;
sides = 12;
System.out.println ("A dodecagon has " + sides + " sides.");
}
}
• The reserved words true and false are the only valid
values for a boolean type
boolean done = false;
8 / 12 equals 0
14 % 3 equals 2
8 % 12 equals 8
a / (b + c) - d % e
2 1 4 3
a / (b * (c + (d - e)))
4 3 2 1
- d
b c
count = count + 1;
• or prefix form
++count
• When used as part of a larger expression, the two forms can have
different effects
• Because of their subtleties, the increment and decrement operators
should be used with care
is equivalent to
num = num + count;
+= 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
is equivalent to
result = result / ((total-MIN) % num);
money = dollars
• Only widening conversions can happen via assignment
• Note that the value or type of dollars did not change
• The nextLine method reads all of the input until the end
of the line is found
import java.util.Scanner;
message = scan.nextLine();
import java.util.Scanner;
(more…)
• Chapter 2 focused on
– character strings
– primitive data
– the declaration and use of variables
– expressions and operator precedence
– data conversions
– accepting input from the user