Lesson 04 - Primitive Data Types I
Lesson 04 - Primitive Data Types I
To know about:
Data and data types.
Naming variables, constants, methods, and classes.
Using of variables to store data.
Using constants to store permanent data.
Programs with assignment statements and expressions.
Using Java operators to write expressions.
Being familiar with Java documentation, programming
style.
Naming conventions in java.
Fundamental Building Blocks of
3
Programs
There are two basic aspects of programming:
Data: To work with data, you need to understand
variables and types.
Instructions: to work with instructions, you need to
understand control structures and subroutines.
Variables and Data Types (1)
4
firstChar
Assignment Statement
8
mark 67
Examples:
int mark = 67; radius 4.4
double radius = 4.4;
float interestRate = 0.3; interestRate 0.3
char firstChar = ‘a’;
firstChar a
Variable Type, Size and Value
9
long b = 4
int z = 4 4
short y = 4 4
byte x = 4
4
4 z
b
y
x
byte short int long
1 byte 2 byte 4 byte 8 byte
Expressions
10
Output f
Stop
Example 1: Writing a Simple Java Program (2)
12
Every java program must start with class keyword and have a
main method.
// Step 3: Display f
}
}
Example 1: Writing a Simple Java Program (3)
13
// Step 3: Display f
}
}
Example 1: Writing a Simple Java Program (4)
14
// Step 3: Display f
}
}
Example 1: Writing a Simple Java Program (5)
15
// Step 3: Display f
System.out.println(f);
}
}
Example 2 (1)
16
radius = 20;
allocate memory
for radius
public class ComputeArea {
public static void main(String[] args) {
double radius; radius no value
double area;
radius = 20;
Assign 20 to
public class ComputeArea { radius
public static void main(String[] args) {
double radius;
radius 20
double area;
area no value
radius = 20;
radius = 20;
Examples:
final double PI = 3.14159;
final int SIZE = 3;
Arithmetic Operators (1)
24
Examples:
1 + 1 evaluates to 2
20 % 3 evaluates to 2
5 / 2 yields an integer 2.
5.0 / 2 result is 2.5
5 % 2 is equal to 1
System.out.println(3 * 4); prints 12
Q: What is the result of (3 * 4 % 5) ?
Q: 1 * 2 + 3 * 5 % 4 = ?
Operator Precedence
26
Algebra: y = mx + b
Java: y = m * x + b;
3 1 2
Algebra: m =
Java: m = (a + b + c + d) / 5;
5 1 2 3 4
Precedence examples (1)
28
Precedence examples (2)
29
1 * 2 + 3 * 5 % 4 1 + 8 % 3 * 2 - 9
\_/ \_/
| |
2 + 3 * 5 % 4 1 + 2 * 2 - 9
\_/
\___/
| |
2 + 15 % 4 1 + 4 - 9
\____/
\______/
| |
2 + 3 5 - 9
\_________/
\________/ |
| -4
5
Precedence Questions
30
Example: 15 / 2 is 7, but
15.0 / 2.0 is 7.5
Real Number Example
32
Sticking with the Java naming conventions makes your programs easy
to read and avoids errors.
Choose meaningful and descriptive names.
a) Variables and method names:
With single word variable names, all characters are lower case
Example: grades, radius, area.
If the name consists of several words, use lowercase for the first word, and
capitalize the first letter of each subsequent word in the name.
Example: computeArea, studentName .
b) Constants:
Capitalize all letters in constants, with more than one word constant use
underscore (_) between the words.
Example: PI, MAX_VALUE.
c) Class names:
Capitalize the first letter of each word in the name.
Example: ComputeArea.
Why Variables?
35
http://introcs.cs.princeton.edu/java/home/
www.prenhall.com/liang
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/data
types.html
http://docs.oracle.com/javase/tutorial/index.html
http://math.hws.edu/javanotes/c1/s4.html
cs.nyu.edu/courses/spring07/V22.0101-002/02slide.ppt
Herbert Schildt, “Java A Beginner’s Guide: Create,
Compile, and Run Java Programs Today,” 5th Ed, page 15.
Deitel & Deitel, “Java How to Program”, 9th ed, chapter 2,
page 37.
References
37