Unit 1: Primitive Data Types
Java Data Types in AP Computer Science A
What are Data Types?
Data types are the building blocks of any programming language. They are the fundamental units of storage that are used to store information in a program. Data types are used to define variables and methods in a program. They also define the operations that can be performed on the variables and methods.
Data Types in AP CS A
Remember that Java is statically typed, which means that the data type of a variable is known at compile time. This is in contrast to dynamically typed languages, where the data type of a variable is known at runtime. In Java, the data type of a variable is specified when the variable is declared. The data type of a variable cannot be changed after it is declared.
The AP Computer Science A curriculum covers the following data types:
- Primitive Data Types
- Reference Data Types
- Specialized Data Types
The AP Computer Science A curriculum covers the following operations on data types:
- Assignment
- Arithmetic
- Relational
- Logical
- Bitwise
- Type Conversion
- Casting
Primitive Data Types
Primitive data types are the most basic data types in Java. They are the building blocks of all other data types. There are eight primitive data types in Java: byte, short, int, long, float, double, char, and boolean. The following table shows the size and range of each primitive data type.
Char and String
- char - A single character (e.g. 'a', 'b', 'c ', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
- String - A sequence of characters (e.g. "Hello World!")
Numeric
- byte - A whole number between -128 and 127
- short - A whole number between -32,768 and 32,767
- int - A whole number between -2,147,483,648 and 2,147,483,647
- long - A whole number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807
Floating Point
- double - A decimal number (e.g. 1.0, 2.0, 3.0, 4.0, 5.0, 6.0)
- float - A decimal number between 1.4E-45 and 3.4028235E38
Boolean
- boolean - A value that is either true of false
Declaring Variables
Variables are used to store data in a program. They are declared using the following syntax:
<type> <name>;
String word;
Assignment
Assignment is the process of assigning a value to a variable. The following table shows the syntax for assignment.
Data Type | Syntax |
---|---|
char | char letter = 'a'; |
String | String word = "Hello World!"; |
byte | byte number = 1234; |
short | short number = 22342; |
int | int number = 2,147,483,645; |
long | long number = 223,372,036,854,775,808; |
double | double number = 5.0; |
float | float number = 6.0; |
boolean | boolean value = true; |
<type> <name> = <value>;
char letter = 'a';
String word = "Hello World!";
byte number = 1234;
short number = 22342;
int number = 2,147,483,645;
long number = 223,372,036,854,775,808;
double number = 5.0;
float number = 6.0;
boolean value = true;
Reference Data Types
Reference data types are data types that are not primitive. They are used to store objects. There are two types of reference data types: class and interface. The following table shows the size and range of each reference data type.
- class - A reference to an object of a class
- interface - A reference to an object of an interface
Specialized Data Types
Specialized data types are data types that are not primitive or reference. They are used to store objects. There are two types of specialized data types: array and enum. The following table shows the size and range of each specialized data type.
- array - A collection of objects of the same type
int[] numbers = {1, 2, 3, 4, 5};
- enum - A collection of objects of the same type. The objects are called enumerators.
enum Color {
RED,
GREEN,
BLUE
}
Common Errors
Common Errors Using Data Types in AP CS A
Some common errors that occur when working with data types in AP CS A are:
- Type Mismatch
- Type Conversion
- Implicit Type Conversion
- Explicit Type Conversion
Type Mismatch
Type mismatch occurs when a variable is assigned a value of a different type than the variable's type. For example, the following code will result in a type mismatch error because the variable number
is of type int
and the value 5.0
is of type double
.
int number = 5.0;
Type Conversion
Type conversion is the process of converting a value of one type to a value of another type. There are two types of type conversion: implicit and explicit. Implicit type conversion is the process of converting a value of one type to a value of another type without the programmer's knowledge. Explicit type conversion is the process of converting a value of one type to a value of another type with the programmer's knowledge.
Implicit Type Conversion
Implicit type conversion is the process of converting a value of one type to a value of another type without the programmer's knowledge. For example, the following code will result in an implicit type conversion error because the variable number
is of type int
and the value 5.0
is of type double
.
int number = 5.0;
Explicit Type Conversion
Explicit type conversion is the process of converting a value of one type to a value of another type with the programmer's knowledge. For example, the following code will result in an explicit type conversion error because the variable number
is of type int
and the value 5.0
is of type double
.
int number = 5.0;
Summary
In this unit, you learned about the following data types:
- Primitive Data Types
- Reference Data Types
- Specialized Data Types
References
AP CSA Homework Assignment
Assignment
Instructions
Create a simple Java program that uses each of the primitive data types in a program. Use each of the reference data types in a program. Use each of the specialized data types in a program. Use each of the operations on data types in a program.
- Create a new Java project called DataTypes.
- Build a program of your choice.
- Use each of the primitive data types in a program.
- Use each of the reference data types in a program.
- Use each of the specialized data types in a program.
- Use each of the operations on data types in a program.
Some examples for projects:
- Build a simple calculator.
- Build a simple mortgage calculator.
- Build a simple BMI calculator.
- Build a simple GPA calculator.
- Build a simple tip calculator.
- Build a simple grade calculator.
- Build a simple retirement calculator.
- Build a simple savings calculator.
- Build a simple loan calculator.
- Build a simple car loan calculator.
- Build a simple compound interest calculator.