1
1.3 DATA TYPES
Unit 1 – Primitive Data Types
AP Computer Science A
1.3 Data Types
2
LEARNING OBJECTIVE
You will be able to
•identify the appropriate data type for a
particular situation.
•declare and initialize variables in Java
1.3 Data Types
3
DATA TYPES
A type is a set of values (e.g. integers, floats, etc..) and a
set of operations (e.g. +, -, *, /, etc..) on them.
• Data types can be categorized as either primitive or reference.
The primitive data types used in this course define the
set of operations for numbers and Boolean (true or false)
values.
1.3 Data Types
4
PRIMITIVE TYPES
The primitive types on the Advanced Placement Computer
Science A exam are:
• int - which store integers (whole numbers like 3, -76,
20393)
• double - which store floating point numbers (decimal
numbers like 6.3, -0.9, and 60293.93032)
• boolean - which store Boolean values (either true or
false).
1.3 Data Types
EXAMPLE 1 - IDENTIFY ANY VARIABLES5
AND THEIR DATA TYPE (STRING, INTEGER,
DOUBLE OR BOOLEAN)
•Lucy needs to keep track of her
store’s inventory and how much
money she has made.
1.3 Data Types
EXAMPLE 2 - IDENTIFY ANY VARIABLES6
AND THEIR DATA TYPE (STRING, INTEGER,
DOUBLE OR BOOLEAN)
•Alex wants to write a program to keep
track of his family members and some of
their characteristics and their health.
He wants to collect their age, weight,
height and calories they consume.
1.3 Data Types
EXAMPLE 3 - IDENTIFY ANY VARIABLES7
AND THEIR DATA TYPE (STRING, INTEGER,
DOUBLE OR BOOLEAN)
• Michelle wants to create a gradebook
software program that will help her
keep up with her students’ grades.
1.3 Data Types
EXAMPLE 4 - IDENTIFY ANY VARIABLES8
AND THEIR DATA TYPE (STRING, INTEGER,
DOUBLE OR BOOLEAN)
• Geraldo is working on a password
generator, a program that will create
unique passwords. He wants to make sure
that the user can specify any limitations
on the password.
1.3 Data Types
9
DEBUGGING
• While creating a program you will, on occasion, make a
mistake.
• There are two types of errors we are concerned with…
Logical Error – an error Compiling Error – an error that
where the program does occurs during compilation,
not do what you want it. meaning the program does not
run at all.
1.3 Data Types
10
VARIABLES
A variable is a piece of the computer's memory that is given a
name and type, and can store a value.
• Like preset stations on a car stereo, or cell phone speed dial:
• Steps for using a variable:
• Declare it - state its name and type
• Initialize it - store a value into it
• Use it - print it or use it as part of an expression
1.3 Data Types
11
DECLARATION
variable declaration: Sets aside memory for storing
a value.
• Variables must be declared before they can be used.
• Syntax:
type <name>;
• The name is an identifier.
• int x; x
• double myGPA; myGPA
1.3 Data Types
12
INITIALIZING
assignment: Stores a value into a variable.
• The value can be an expression; the variable stores its
result.
• Syntax:
name = expression;
x 3
• int x;
x = 3;
• double myGPA;
myGPA 3.25
myGPA = 1.0 + 2.25;
1.3 Data Types
13
DECLARATION/INITIALIZATION
• A variable can be declared/initialized in one statement.
• Syntax:
type name = value;
• double myGPA = 3.95; myGPA 3.95
• int x = (12 - 3) * 2; x 18
1.3 Data Types
14
USING VARIABLES
• Once given a value, a variable can be used in expressions:
int x;
x = 3;
System.out.println("x is " + x); // x is 3
System.out.println(5 * x - 1); // 14
• You can assign a value more than once:
int x;
x = 3;
System.out.println(x + " here"); // 3 here
x = 4 + 7;
System.out.println("now x is " + x); // now x is 11
1.3 Data Types
15
ASSIGNMENT AND ALGEBRA
• Assignment uses = , but it is not an algebraic equation.
= means, "store the value at right in variable at left"
• The right-side expression is evaluated first,
and then its result is stored in the variable at left.
• What happens here?
int x = 3;
x = x + 2; // no solutions
// mathematically
// not an equation!
1.3 Data Types
16
MULTIPLE VARIABLES
• Multiple variables of the same type can be declared and
initialized at the same time.
• Syntax:
type name1, name 2, name3;
type name1 = value1, name2 = value2, name3 = value3;
int x, y, z; // declare three integers.
int a = 1, b = 2, c = 3; // declare and initialize
// three integers.
1.3 Data Types
17
ASSIGNMENT AND TYPES
• A variable can only store a value of its own type.
• int x = 2.5; // ERROR: incompatible types
• An int value can be stored in a double variable.
• The value is converted into the equivalent real number.
• double myGPA = 4;
1.3 Data Types
18
COMPILER ERRORS
• Order matters.
int x;
7 = x; // ERROR: should be x = 7;
• A variable can't be used until it is assigned a value.
int x;
System.out.println(x); // ERROR: x has no value
• You may not declare the same variable twice.
int x;
int x; // ERROR: x already exists What is wrong with
this code?
int x = 3;
int x = 5; // ERROR: x already exists
1.3 Data Types
19
PRINTING A VARIABLES VALUE
• Use + to print a string and a variable's value on one line.
double grade = (95.1 + 71.9 + 82.6) / 3.0;
System.out.println("Your grade was " + grade);
int students = 11 + 17 + 4 + 19 + 14;
System.out.println("There are " + students +" students in the course.");
Output:
Your grade was 83.2
There are 65 students in the course.
1.3 Data Types