Topic2-Basic Data Types and Operations
Topic2-Basic Data Types and Operations
1
Learning Outcomes
• At the end of this topic, student should be able to:
2
Identifiers
• An identifier is a sequence of characters that consist of
letters, digits, underscores (_), and dollar signs ($).
• An identifier must begin with a letter, an underscore (_),
or a dollar sign ($).
• An identifier cannot:
– be a reserved word, e.g. true, false, or null
– start with a digit
– contain a (blank) space
– contain special characters, except _ and $
• An identifier can be of any length.
3
Identifiers
Examples
Valid identifiers: Invalid identifiers:
$2 2A
ComputeArea *salary
area gross-salary
radius class
showMessageDialog public
payRate String
salary income tax
J42Stop
gross_salary
4
Identifiers
Conventions for choosing identifier names:
• Name according to what it represents
• Consistent in the use of variable names
• Consistent when using upper and lowercase characters
– Variable name – lowercase, or mix-case
– Constant name – uppercase (all caps)
5
Data
• Data used in processing
• Constant is a value that never changes during the
processing of all instructions in a solution
– Constant is given a location in memory and a name.
• Variable – value may change during processing
6
Constants and
Variables on the Computer
7
Constants
• Represents permanent data that never changes.
• Use keyword (reserve word) final
8
Constants and
Variables on the Computer
9
Variable Names
10
Rules for Naming and Using
Variables
1. Name a variable according to what it
represents.
2. Do not use spaces.
3. Start a variable name with a letter.
4. Do not use a dash or any other symbol
that is used as a mathematical operator.
11
Rules for Naming and Using
Variables
5. Consistent usage of variable name.
6. Consistent use of upper, lowercase characters in
variable names
7. If the name consists of several words, concatenate all in
one, use lowercase for the first word, and capitalize the
first letter of each subsequent word in the name. For
example, the variables radius and area, and the
method computeArea.
8. Use naming convention specified by your company
12
Naming Conventions, cont.
• Choose meaningful and descriptive names
– Constants:
• Capitalize all letters in constants, and use underscores to
connect words. For example, the constant PI and
MAX_VALUE
– Class names:
• Capitalize the first letter of each word in the name. For
example, the class name ComputeArea.
13
Processing Data—How a
Computer Balances a
Checkbook
14
Data Types
• Data are unorganized facts
• Goes as input and is processed to produce output,
information
• Computers must be told the data type of each variable
and constant
• Three common types: numeric, character, and logical
• Other data types: date data type and user-defined data
types
15
Data Types
and Their Data Sets
16
Examples of Data Types
0-17
Examples of Data Types
0-18
Declaring Variables
int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;
19
Assignment Statements
x = 1; // Assign 1 to x;
20
Declaring and Initializing
in One Step
From previous example:
int x;
double radius;
char a; int x = 1;
double radius = 1.0;
x = 1;
char a = ‘A’;
radius = 1.0;
a = ‘A’;
21
Declaring and Initializing
in One Step
• Example:
– Declare all variables involved in the following
problem:
• Find the sum and difference of two integer
numbers where the value of the first integer is 20
and the second integer is 35.
22
Numerical Data Types
Name Range Storage Size
Positive range:
4.9E-324 to 1.7976931348623157E+308
23
Numerical Data Types
byte 8 bits
short 16 bits
int 32 bits
long 64 bits
float 32 bits
double 64 bits
24
Numerical Data Types
Name Range Storage Size
Positive range:
4.9E-324 to 1.7976931348623157E+308
25
Operators and Their Computer
Symbols
26
Mod/Remainder Operator
Remainder is very useful in programming. For example,
an even number % 2 is always 0 and an odd number % 2
is always 1. So you can use this property to determine
whether a number is even or odd. Suppose today is
Saturday and you and your friends are going to meet
in 10 days. What day is in 10 days? You can find that
day is Tuesday using the following expression:
27
Augmented Assignment Operators
28
Increment and
Decrement Operators
29
Increment and
Decrement Operators, cont.
30
Operators and Their Computer
Symbols
31
Definitions of the Logical
Operators
32
Definitions of the Logical
Operators
33
Definitions of the Logical
Operators
34
Hierarchy of Operations
35
Hierarchy of Operations
36
Expressions and Equations
37
Evaluating a Mathematical
Expression
0-38
Evaluating a Relational
Expression
39
Evaluating a Logical Expression
40
Evaluating an Equation That
Uses Both Relational and
Logical Operators
41
Developing a Table of All Possible
Resultants of a Logical Expression
• One unknown—A.
• Two combinations: A can be either True or
False.
42
Developing a Table of All Possible
Resultants of a Logical Expression
• Two unknowns—A and B.
• Four combinations: B can be either True
or False for each value of A..
43
Developing a Table of All Possible
Resultants of a Logical Expression
• Three unknowns—A, B, and C.
• Eight combinations.
44
Developing a Table of All Possible
Resultants of a Logical Expression
45
Introducing Java Programming
with an Example
Computing the Area of a Circle
This program computes the area of the circle.
ComputeArea
46
animation
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}
47
animation
// Assign a radius
radius = 20;
allocate memory
for area
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}
48
animation
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}
49
animation
// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}
50
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);
}
}
51
Reading Input from the Console
1. Create a Scanner object
Scanner input = new Scanner(System.in);
ComputeAreaWithConsoleInput Run
ComputeAverage Run
52
Reading Numbers from the
Keyboard
Scanner input = new Scanner(System.in);
int value = input.nextInt();
Method Description
54
Getting Input Using Scanner
import java.util.Scanner;
public class ReadInput {
public static void main(String[] args) { Create scanner object ONCE,
can be used many times
Scanner scanner = new Scanner(System.in);
// Read input from user
System.out.print("Enter radius: ");
double radius = scanner.nextDouble();
System.out.print("Enter an integer number: ");
int number = scanner.nextInt();
// …. More statements…
}
}
55
Common Error 1:
Undeclared/Uninitialized Variables
and Unused Variables
double interestRate = 0.05;
double interest = interestrate * 45;
56
Common Error 2: Integer Overflow
57
Common Error 3: Round-off Errors
System.out.println(1.0 - 0.9);
58
Common Error 4: Unintended
Integer Division
int number1 = 1; int number1 = 1;
int number2 = 2; int number2 = 2;
double average = (number1 + number2) / 2; double average = (number1 + number2) / 2.0;
System.out.println(average); System.out.println(average);
(a) (b)
59
Common Pitfall 1: Redundant
Input Objects
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int v1 = input.nextInt();