String
String
\b backspace
\t tab
\n newline
\r carriage return
\" double quote
\' single quote
\\ backslash
• See Roses.java
2
More on String Objects
• Because strings are so common, we don't have
to use the new operator to create a String
object
title = "Java Software";
• which is a shorcut for:
title = new String ("Java Software“);
title “Java Software"
9282
9283
9284
A computer can use more cells
9285
to store data, e.g., 2 bytes
9286
- how many values can 2 bytes represent?
Numeric Primitive Data
• “Objects” of different numeric data types
occupy different number of cells
Type Storage Min Value Max Value
float 32 bits +/- 3.4 x 1038 with 7 significant digits IEEE 754
double 64 bits +/- 1.7 x 10308 with 15 significant digits format
Characters
• A char is a single character from a
character set
• A character set is an ordered list of
characters; each character is given a
unique number
• Character literals are represented in a
program by delimiting with single quotes:
13
Variables: Revisited
• We already know that a variable must be
declared, specifying the variable's name and
the type of information that will be held in it
• As of now, think of a variable as a name for a
location in memory cell (we will revisit the
concept later)
data type variable
name
int total;
int sum = 0;
int base = 32, max = 149;
String msg1 = new String( “Hello” );
String msg2 = “Hello” ;
• When a variable is referenced in a program,
its current value is used
• See PianoKeys.java
Change the Value of a Variable:
Assignment Statement
• An assignment statement changes the value of
a variable
• The assignment operator is the = sign
total = 55;
• See Geometry.java 16
Constants
• A “constant variable” is an identifier that is similar to a
variable except that it holds one value for its entire
existence
• Why constants:
– give names to otherwise unclear literal values
– facilitate changes to the code
– prevent inadvertent errors
• In Java, we use the final modifier to declare a
variable constant, and the convention is to use all
capital words to name a constant
final double PI = 3.14159265;
• The compiler will issue an error if you try to assign
value to a constant variable after its declaration
Arithmetic Expressions
• An expression is a combination of operators and
operands
• Arithmetic expressions (we will see logical expressions
later) are essentially special methods applied to
numerical data objects: compute numeric results and
make use of the arithmetic operators:
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
Division and Remainder
• If both operands to the division operator (/) are integers, the result
is an integer (the fractional part is discarded)
14 / 3 equals?
8 / 12 equals?
14 % 3 equals?
8 % 12 equals?