The AP Computer Science A 5-hour live stream review is here!Β πŸ’»

Join us on May 5, 2021 for the 🌢️ AP Computer Science A Cram Finale for a last minute review to get all your questions answered!


All Subjects

Β >Β 


AP Comp Sci A

Β >Β 


Unit 1

1.1 Unit 1: Primitive Types

8 min readβ€’november 16, 2020

Peter Cao

Unit 1: Primitive Types

The Big Takeaway From This Unit

Variables and Primitive Data Types All information in Java can be stored in variables and can take the form of primitive data types or combinations made from these primitive data types.

Unit Overview

Exam Weighting

  • 2.5-5% of the test
  • Roughly 1 to 2 multiple-choice questions (Note: While not much in this unit is tested directly, it is the foundation for all other units on the test)

Enduring Understanding

Welcome to AP Computer Science A, or AP CSA for short! In this course, you will learn the basics of Java, a programming language that is widely used today. In these guides, you will not only learn how to code but also how to code well.

Image from GIPHY

Unit 1 in AP CSA is named "Primitive Types," and they are the building blocks of the rest of Java. The types you have to know in this class are the boolean, float, and double, although there are others. However, it is not the only thing you'll learn in this unit. You'll also learn why we use Java, and you'll make basic programs as well! In making these basic programs, you'll get a little taste of classes, which are the big overarching concept tying this course together!

Building Computational Thinking

When we program, a lot can be represented mathematically, with comparison, equality, and arithmetic. Using this, it is easier from start to finish to trace programs with this intuition to figure out what the output is. We'll also build programs from a specification, with instructions with what to do. From this, we'll also learn about basic input and output. There will be errors when we first learn to program, and thus we'll need to get acquainted with some of the more common errors that will occur when programming.

Main Ideas For This Unit

  • The functions and purposes of using Java
  • Basic input and output
  • Java arithmetic and operators
  • Primitive types
  • Variables

1.1: Why Programming? Why Java?

Intro to Programming

Almost all the technology we use today, from everything on your computer to your smartwatch to your television, is made possible by programming. Programming is what makes these devices respond to your actions and commands. What is programming anyway? In short, it is giving a device instructions to respond to different actions and run. There's a lot more complexity to this involving computer hardware, but that is beyond the scope of this course. In essence, our computer is "run" using bits of 0s and 1s, which represent on and off electrical signals. Some programming languages, such as Assembly, interact almost directly with those bits. However, Assembly is inconvenient to program in and is incredibly hard to read.

Courtesy of Android Authority

Thus, a need for higher-level programming languages that are easier to code in and understand was born. First came languages like Pascal and FORTRAN, then C, C#, and C++, common mainstays in the programming world. The most popular programming languages of Javascript, Python, and the focus of this course: Java, have only come about recently. These newer languages are much easier to understand and look like everyday English to an extent. For machines to understand this code, however, the program must be converted into machine code, which is what computers understand. As you progress, you'll learn that Each language has a different way of doing this.

Courtesy of MalwareBytes.

What Is Java, How Does It Work, and Why Do We Use It?

Java is one of the high-level programming languages mentioned earlier. It is also the second most used programming language in the world, behind Python. There are several reasons that Java is so popular, and here's a list from Cay Horstmann's *Core Java *****based on the original Java developers' claims:
  1. Simple As a high-level language, the syntax for Java is much simpler than languages like C and related languages and thus is easier to read.
  2. Object-Orientated Java is an object-orientated language, with its emphasis on classes, objects, and methods fundamental to writing code in Java. Object-orientated programming is code written that manipulates these classes, objects, and methods and is central to this course.
  3. Distributed Java can easily connect with the internet and file systems to easily access information either from files or APIs.
  4. Distributed Java can easily connect with the internet and file systems to easily access information either from files or APIs.
  5. Secure Java is built to be resistant to many different attacks, which makes sense as one of its original intents is to be connected to networks.
  6. Architecture-Neutral One of the most common sayings of Java is "Code once, run anywhere" because once it is compiled, it does not depend what device it is on, it will run, since the bytecode is run on the Java Virtual Machine (JVM) which is the same on every device. This is a contrast to earlier programming languages which will only run on the device it was coded on.
  7. Architecture-Neutral One of the most common sayings of Java is "Code once, run anywhere" because once it is compiled, it does not depend what device it is on, it will run, since the bytecode is run on the Java Virtual Machine (JVM) which is the same on every device. This is a contrast to earlier programming languages which will only run on the device it was coded on.
  8. Interpreted The JVM acts as an interpreter, which can quickly use the bytecode and return an output. However, Java is still mainly a compiled language, as opposed to Python, which is an interpreted language, where the code is immediately turned into output without any intermediary files. However, once we have the compiled code in Java, it will run quicker than code that needs to be reinterpreted every time in Python.
  9. High-Performance The modern compilers of Java are quick and can compile large blocks of code quickly.
  10. Multithreaded Java was the first modern language to support parallel programming, which is doing multiple processes, called threads at a time. This will be useful for multi-core processors allowing for stronger computers.
  11. Dynamic There are many libraries that can be added to the Java in order to increase its functionality such as adding new classes, objects, and methods. We'll be looking at a few of them in this course.

Getting Ready for Your First Program

Before we start programming, we have to actually set up our coding environment. To do this, we use an Integrated Development Environment (IDE). Your teacher will usually have a preference, but if they don't, any of these will work perfectly fine. A few well-known ones for Java include:
  • BlueJ

Courtey of BlueJ.

However, in these guides, code will either be embedded directly onto the site or be from, an online IDE that takes advantage of remote compilers so that your computer's RAM isn't overwhelmed.

Courtesy of

The white half is the code editor, where you enter your code that is compiled and later executed. The blue half is the console, which shows the output of the program and any errors that occur. On the left of the screen is the file explorer (not visible above), and this is where your different code files (which are your Java classes, which we’ll discuss later) are located on your computer.

The Anatomy of a Java Program

Below is the first program many students learning Java will see. There seems to be a lot going on, but here, we'll break it down!
public class Main { public static void main(String[] args) { System.out.println("Hello world!"); } }
Do you see the indentation? Indentation isn't needed but it can make your code much easier to read! I recommend proper indentation when coding by either using a tab or 2 spaces. Just remember to be consistent!
If you hit run, the console will return:
> javac -classpath .:/run_dir/junit-4.12.jar:target/dependency/* -d . > java -classpath .:/run_dir/junit-4.12.jar:target/dependency/* Main Hello world!
There is a lot that's going on in these snippets, and we'll learn these more in-depth in later units. In the console, we first see javac, which is the Java compiler compiling, and then a java command executing the Main bytecode. The third line (and beyond, depending on the program) has the result of the code. In future instances, we won't show the first two lines.
Meanwhile, on the code, let's decode it line by line! The java files are always in the name, and in this case, the file in question is thus the class Main. The public means that it can be accessed from any other class. The {} denotes the contents inside the class and the main method after itβ€”in other programming languages, they are called functionsβ€”with the method header in line 2. Every program, no matter how many classes there are, must have a main method in only one of the classes. The main method is "the master switch," the method that determines what other methods are called. The static means that we don't need an instance of the "Main" class (we'll learn more about instantiations and objects in Unit 2). The void signifies that the method does not return anything directly to another function. Then, the method name is next, and any parameters in parentheses. In this case, the parameters (String[] args) is an array of strings named args (we'll learn more about arrays in Unit 6). The third line is what does some work. The System.out.println("Hello world!"); prints Hello world!, and sets the cursor to a new line if there is anything else to print. Note the semicolon after the line; this is necessary as it shows the end of an action. If it isn't there, the compiler returns a syntax error. "Hello world!" is what is known in Java as a string literal, a string for short. A string is an object in Java that sets up a template for strings. Any time there is a statement enclosed in double-quotes, that is a string.

System.out.println() vs System.out.print()

There are two ways to print text to the consoles that we'll use in this course. They are both methods of the System.out object, which is a part of the System class. System.out.println() prints whatever is in the parentheses to the console and sets the cursor on a new line. On the other hand, System.out.print() prints whatever is in the parentheses, but doesn't set the cursor on a new line.


public class Main { public static void main(String[] args) { System.out.print("Hi! "); System.out.println("Hello!"); System.out.println("Bye!"); } } Hi! Hello! Bye!

Was this guide helpful?

πŸ’ͺ🏽 Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Browse Study Guides By Unit
Exam Reviews
Unit 10: Recursion
Unit 2: Using Objects
Unit 3: Boolean Expressions and if Statements
Unit 4: Iteration
Unit 5: Writing Classes
Unit 6: Array
Unit 7: ArrayList
Unit 8: 2D Array
Unit 9: Inheritance
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now