Unit 1.2: Variables and Data Types
Variables
In Algebra lessons, you would have used variables.
Let us write a simple equation in math to calculate the mean of a set of numbers:
a = 15, b = 35, c = 50
mean = (a + b + c) / 3 = 33.33
You see several variables here: a, b, c, and finally the mean.
To do this simple calculation, you may be using mental math or a calculator. But if you are writing a Java program to do so, then you first have to understand how to declare variables a, b, c, and mean, understand the data types (integers, real numbers, text, etc.) that can be assigned to your variables, and finally, understand the various arithmetic operators that you can use.
A variable is a named storage location for a value. All variables must be declared before they can be used. A variable name should be preceded by the data type.
- Variables act as named containers for data. Declaring a variable tells the computer to set aside memory for a specific type of information.
Here is the equivalent Java code for performing this simple operation:
Task: Declaring and initializing variables for a math calculation.
int a = 15;
int b = 35;
double c = 50;
double mean = (a + b + c) / 3;
In the example above, a and b are declared with the int data type because they represent whole numbers. However, c is declared as a double even though it is assigned a whole number (50). This is a strategic choice: in Java, if all numbers in a division are integers, the result is truncated (the decimal part is thrown away). By making at least one variable (like c) a double, we force Java to use floating-point math, ensuring the mean is calculated accurately with decimals. Finally, mean is a double because the average of a set of numbers is frequently a decimal value.
What is a Data Type?
A data type is a classification that specifies which type of value a variable can hold, as well as the set of operations (like addition or subtraction) that can be performed on it.
- Data types define the "nature" of the data being stored. They determine how much memory is used and what logic can be applied to that data.
A variable's data type determines the type and size of values it may contain. In Java, data types are categorized into two main groups:
- Primitive Types: The most basic data types. They are not objects.
- Reference Types: Used to define objects, which are more complex data structures.
Primitive Data Types
Primitive types are the most basic data types. They store simple values directly in memory, like the example given below:
Task: Declaring and initializing different primitive data types.
// 'age' is a primitive type (int). It holds a simple, direct value.
int age = 25;
// boolean is also a primitive type.
// It can only have a true or false value.
boolean isTodaySunny = true;
// When you have a decimal value to store, you use a double.
double pi = 3.14;
The three primitive types you will use most often are int (integers), double (real numbers), and boolean (true/false).
Reference Data Types
Reference types are used to store complex objects. Unlike primitive types, which hold the value directly, reference types hold the address of the object in memory. String is the most common reference type you will use early on.
Task: Declaring reference types like Strings and Arrays.
// String is a reference type. It stores text.
String name = "Alice";
// Arrays are also reference types, even if they hold primitives.
int[] numbers = {1, 2, 3};
- The rules for creating variable names are similar to the rules for creating identifiers as described in the previous chapter. However, from the naming conventions perspective, variable names are noun forms (similar to class names) and start with a lowercase letter except for constants. Constants use uppercase letters and underscores to separate words.
- Constants are a special type of variable which are assigned a value that does not change during the entire execution of the program. In Java, we use the
finalkeyword to declare a constant, which prevents its value from being modified once it is assigned. When declaring a constant, thefinalkeyword must come before the data type. For example, to keep the value ofPI(which never changes), you could create a constant and assign the value3.14once and it never changes after that:
final double PI = 3.14;
As a convention, you typically keep all variable declarations and initialization as the first set of statements in a block of code.
Reference Types vs. Primitive Types
The key difference lies in what a variable stores in memory:
- A primitive variable stores the actual value (e.g., the number
25). - A reference variable stores the memory address of where the object's data is located.
You can think of a primitive variable as a box holding a value, and a reference variable as a signpost pointing to where the value is.

Passing Reference Types
A reference type stores a reference (or memory address) to where an object is located. When a reference type is passed to a method, it is passed by reference, meaning the method receives the memory address and can modify the original object's data. This is different from primitives, which are passed by value (a copy is sent). You will learn more about this when learning about objects.
Variable Initialization
A local variable (one declared inside a method) must be initialized (assigned a value) before it can be used. If you declare an instance or static variable without initializing it, it gets a default value (0, 0.0, false, or null depending upon the data type: int, float/double, boolean, or object). You will learn more about instance and static variables later in the course.
Assignment Operator
The assignment operator (=) stores the value of the expression on its right into the variable on its left.
- In programming,
=does not mean equality; it means "take the value from the right and store it in the variable on the left."
Task: Separating declaration and assignment.
int score; // Declaration
score = 100; // Assignment
In the above code snippet:
- The first variable
scoreis declared to be of typeint.- In the next line, it is assigned (a.k.a. initialized) the value
100.- If you try to assign a value before declaration, the Java compiler throws an error as, before any assignment, it must first be declared.
- Initialization is the way to give a value to a variable. If given no value, Java assumes that the value is "0" for integer and decimal types, false for the boolean type, and null for String types.
Example of declaration and assignment in the same line
Task: Declaring and initializing a variable in a single statement.
int b = 15;
In the above example ‘b’ is a variable, declared to be of type int and is assigned the value 15. Both declaration and assignment are in the same statement. If you already know the value to initialize with when a variable is declared, then this is the preferred way of coding: declare the variable and initialize the value in the same statement instead of using two separate statements.
Expressions
An expression is a combination of values, variables, and operators that evaluates to a single value.
- Expressions are evaluated by the CPU. The final calculated value can then be used in assignments or output statements.
Task: Evaluating an arithmetic expression.
int a = 10;
int b = 3;
int result = a * b + 5; // The expression "a * b + 5" evaluates to 35
This is no different from the Algebra expressions that you are already used to.
More on Primitives
In all, Java supports the following primitive data types.
int: The int data type is a 32-bit signed two's complement integer. It can hold a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice.
double: The double data type is a 64-bit decimal number. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information.
EXCLUSION STATEMENT—The five primitive data types (long, short, byte, float, and char) are outside the scope of the AP Computer Science A course and exam. You can skip the below content as appropriate
- byte: The byte data type is an 8-bit signed two's complement integer. It can hold a minimum value of -128 and a maximum value of 127. Default value is 0.
short: The short data type is a 16-bit signed two's complement integer. It can hold a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
long: The long data type is a 64-bit signed two's complement integer. It can hold a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int. If an integer value ends with the letter L or l, then it is long; otherwise, it is of type int. You can use either uppercase ‘L’ or lowercase ‘l’ when defining a long value. It is recommended that you use the uppercase letter L because the lowercase letter l is hard to distinguish from the digit 1.
float: The float data type is a 32-bit decimal number. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. Use a float (instead of double) if you need to save memory in large arrays of floating-point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead.
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
More Example Declarations
Here are some declarations:
Task: Reviewing variable declarations for different types.
short myAge ;
double price ;
String myName ;
boolean likesJava ;
More Examples
int used to represent integers
int year = 2003;
double used to represent numbers with a decimal point
double price = 12.45 ;
String is used to represent text - a sequence of characters.
String myName = "Jayashree Ravi" ;
String gameType = "Modding101";
boolean is either true or false
boolean isLearningJava = true ;
boolean likesIceCream = false ;
All the above types are also called primitive data types except for String. String is considered an object.
- Before you assign a value to a variable, it should first be declared.
- When you declare a variable and do not assign a value, the default value is 0 for all numerical types, the false value for the boolean type, and null for the String type. You will learn about Strings in the next chapter.
- Before you choose a type, think through the maximum and minimum values that a variable may be assigned.