1.2 Variables and Data Types
Part 1
Enduring Understanding
To find specific solutions to generalizable problems, programmers include variables in their code so that the same algorithm runs using different input values.
Learning Objective
Identify the most appropriate data type category for a particular specification.
Essential Knowledge
A type is a set of values (a domain) and a set of operations 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 values.
Introduction
In Java, data types define the type and size of values that can be stored in variables or returned by methods. Java has two main categories of data types: primitive types and reference types.
Primitive Data Types:
byte
: Represents 8-bit signed integers. Range: -128 to 127.short
: Represents 16-bit signed integers. Range: -32,768 to 32,767.int
: Represents 32-bit signed integers. Range: -2,147,483,648 to 2,147,483,647.long
: Represents 64-bit signed integers. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.float
: Represents single-precision 32-bit floating-point numbers.double
: Represents double-precision 64-bit floating-point numbers.char
: Represents a single Unicode character. Range: 0 to 65,535.boolean
: Represents a boolean value (true
orfalse
).
Reference Data Types:
String
: Represents a sequence of characters.Array
: Represents an ordered collection of elements.Interface
: Represents an interface type.Class
: Represents a class type.Enum
: Represents an enumeration type.Object
: Represents the base class for all other classes in Java.
Java also allows the creation of user-defined reference types through classes and interfaces.
Data types in Java have certain characteristics:
Size: Primitive data types have fixed sizes, while reference types have variable sizes based on the data they hold.
Default Values: Variables without an assigned value have default values assigned based on their data type. For example,
int
defaults to 0,boolean
defaults tofalse
, and reference types default tonull
.Type Safety: Java is a statically typed language, which means variables must have a declared type, and type checking is performed at compile-time to ensure compatibility and prevent type errors.
It's essential to choose appropriate data types based on the requirements of your program to ensure efficient memory usage and accurate representation of data.
Part II
Learning Objective
Declare variables of the correct types to represent primitive data.
Essential Knowledge
The three primitive data types used in this course are int, double, and boolean.
Each variable has associated memory that is used to hold its value.
The memory associated with a variable of a primitive type holds an actual primitive value.
When a variable is declared final, its value cannot be changed once it is initialized.
Introduction
int
Examples:
In the examples above, age
, quantity
, and temperature
are variables of type int
, storing integer values.
double
Examples:
In the examples above, pi
, money
, and degrees
are variables of type double
, storing decimal or floating-point values.
boolean
Examples:
In the examples above, isRaining
, isSunny
, and hasPassedExam
are variables of type boolean
, storing true
or false
values to represent logical conditions.
Note: In Java, variable names are user-defined, and they can be chosen according to the context and purpose of the program. The examples above use descriptive names for better understanding, but you can choose appropriate variable names based on your specific use case.
final Example
When a variable is declared as final
, its value cannot be modified after initialization. The variable must be assigned a value either during its declaration or within the constructor of the class. Once initialized, you cannot reassign a new value to it.
Last updated
Was this helpful?