Verified for the 2025 AP Computer Science A exam•Citation:
In Java, everything is either one of several primitive data types or combinations of these primitive data types (collections and objects). These combinations of primitive data types are called reference data types.
For this class, you have to know these three:
Each of the primitive data types comes with its own set of defined operations. These are future topics within the course. With these three primitive types and their operations, we can do many mathematical and logical operations.
Variables are one way to store data in Java and are used again and again in method parameters and different operations as well. To initialize (make) a variable, we do this:
(scope) dataType variableName = dataValue
The special capitalization is called Camel Casing, where the first word is lowercase while the rest are capitalized and combined into one word. Variable and method names use Camel Casing. Class names use a different approach called Pascal Casing, where all words are capitalized.
Variable names must follow these rules:
When naming your variable, using descriptive variable names are preferred instead of a random name like "d" or "sksksksksksk"." "i" and "j" are allowed variable names that have a specific purpose, but we will cover this later.
Also, notice how each variable has its data type. This is called a strongly typed language, more specifically statically typed. This is the opposite of a dynamically typed language, where variables can change partway through.
Examples of legal and illegal variable names:
Good names: iceCreamFlavor, carMake, name Legal, but not preferred: $ball, j, var4 Illegal: nf 23f, &jjjjd, 2fdjjkdk
Here is an example of initializing variables of all of the data types. The equal sign is the assignment operator that assigns/gives a value to the variable.
int daysInWeek = 7; private String name = "Bob"; protected double tax = 0.0775; public boolean goalMet = false;
Note that there are sometimes scope modifiers with the variables, also called access modifiers. If there are no scope or access modifiers, this is called default access, which means the variable is accessible by all classes in that package (also known as a folder in your computer).
Private access is where a variable, class, or method is only available to that own class. We already talked about public access when talking about the main method in 1.1. Finally, protected access with methods, variables, and classes is available to all classes in that package and any subclass as well (we will talk about subclasses in unit 9).
We can also declare the variable without initializing it like this:
private String name; private int age; private double gpa;
Some variables are constants, which means that they never change. We use the final modifier to show this and name the variable in all capitals. Here is how we denote them in Java:
final double TAX;
At some point in your AP CSA experience, you are going to have to do input. In Java, there are many ways to do input and you can see them here, but we're going to focus on using the Scanner class.
import java.util.Scanner; public class ScannerTest { public static void main(str[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); public int sampleInputInt = input.nextInt(); System.out.print("Enter a double: "); public double sampleInputDouble = input.nextDouble(); System.out.print("Enter a boolean: "); public boolean sampleInputBoolean = input.nextBoolean(); System.out.print("Enter a String: "); public String sampleInputString = input.nextLine(); System.out.print("The string you entered was "); System.out.println(sampleInputString); System.out.print("The integer you entered was "); System.out.println(sampleInputInt); System.out.print("The double you entered was "); System.out.println(sampleInputDouble); System.out.print("The boolean you entered was "); System.out.println(sampleInputBoolean); input.close(); } }
To use the import shown above, you have to follow the following steps:
You can see the scanner method receives data in the snippet above. If you enter the wrong data type, the program will crash, and you have an InputMismatchException. In the final print statement, you can see that we put a variable in the parameter, and it will print the value saved in the variable. After, we close the scanner object before we end the program. Here is an example of the console output with user input in italics.
Enter an integer: *7* Enter a double: *3.14159* Enter a boolean: *true* Enter a String: *Hi.* The string you entered was Hi. The integer you entered was 7 The double you entered was 3.14159 The boolean you entered was true
Note that we can put non-String types into the print statement and converts these to a string. You will learn how in future units.