Chapter 2 - Review - Part 1
Chapter 2 - Review - Part 1
2.4 Identifiers
2.5 Variables
2.6 Assignment Statements and Assignment Expressions
2.3 Reading Input from the Console
2.7 Named Constants
2.5 Variables
Declaration Statements
int x; // Declare x to be an integer variable
double radius; // Declare radius to be a double variable
char y; // Declare y to be a character variable
A variable is a container which holds the value while the Java program is executed.
It is like a box in the memory to store a value in it.
To declare (create) a variable, you will specify the type, leave at least one space, then
the name for the variable and end the line with a semicolon ( ; ). For example: int x;
the statement creates a variable named x that can hold an integer value.
double radius; creates a variable named radius that can hold a decimal value. So
Java uses the keyword int for integers, double for a floating point numbers, and char
for characters.
A variable cannot be used in a program unless it has been declared.
Now we haven’t initialized the variables. The declaration gives a name and a data
type for the variable. These containers are empty.
2
2.4 Identifiers
String name = "Hello"; double score = 12.7; int age = 51; boolean answer= true;
Assignment Statements
x = 1; // Assign 1 to x
radius = 1.0; // Assign 1.0 to radius
y = 'A'; // Assign 'A' to y
Equivalent to
int x = 1;
int x;
char y = 'A'; x=1;
double radius = 1.0;
4
Assignment expression
int a, b;
int a, b; Is equivalent to
a b int a;
int b;
12
b
a = 12;
a
7
b = 7;
b
3
a = 3; 12
a 3+7=10
Note: You can change the value of a variable (changed a from 12 to 3).
5
Concatenation operator (+)
int a, b;
Output on the Screen:
a = 3; The value of c is 10
b = 7;
b
int c = a + b;
System.out.println("The value of c is " + c );
//System.out.println("The value of c is " c); Syntax error
7
Trace a Program Execution
public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
double radius; radius no value
double area; area no value
// Assign a radius
radius = 20;
allocate memory
// Compute area for area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
8
animation
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
9
animation
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
10
animation
// Compute area
area = radius * radius * 3.14159; print a message to the
console
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
11
Read an input value from the user using Scanner
public class ComputeArea {
public static void main(String[] args) {
double radius;
double area;
// Assign a radius
radius = ??????????;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " + radius + " is " + area);
}
}
12
Getting Input Using Scanner
Sample Run
13
2.7 Named Constants
14
animation
// Compute area
area = radius * radius * PI;
// Display results
System.out.println("The area for the circle of radius " + radius + " is " + area);
}
}
15