๐Ÿ“š

All Subjects

ย >ย 

๐Ÿ’ปย 

AP Comp Sci A

ย >ย 

โž•

Unit 1

1.1 Unit 1: Primitive Types

8 min readโ€ขnovember 16, 2020

peter-cao

Peter Cao


AP Computer Science Aย ๐Ÿ’ป

Bookmarkedย 2.4kย โ€ขย 93ย resources
See Units

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.
https://media.giphy.com/media/13GIgrGdslD9oQ/giphy.gif

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.
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-4M8zSN6hjkw5.png?alt=media&token=62ee9ea1-7a38-4fe0-84d4-01850c7518c2

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.
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-JnZT2lFkrcnB.png?alt=media&token=501feab9-99be-4f4d-9ecc-0b6ef3389a46

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
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-3xaqPSxXoI5Z.png?alt=media&token=a953e645-e7d4-4d92-973d-db9304d11eaa

Courtey of BlueJ.

However, in these guides, code will either be embedded directly onto the site or be from repl.it, an online IDE that takes advantage of remote compilers so that your computer's RAM isn't overwhelmed.
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-vs4eEccHbCLT.png?alt=media&token=a03a80ef-8b91-4442-9e59-6aa1b177b176

Courtesy of Repl.it.

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 . Main.java > 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 Main.java, 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 ClassName.java, and in this case, the file in question is Main.java 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.

Example

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?

Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
๐Ÿ” Are you ready for college apps?
Take this quiz and find out!
Start Quiz
Browse Study Guides By Unit
๐Ÿ™Exam Reviews
๐Ÿ–ฑUnit 10: Recursion
โž•Unit 1: Primitive Types
๐Ÿ“ฑ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
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
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
๐Ÿ’ช๐Ÿฝ 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!
START QUIZ
๐Ÿ“ฑ Stressed or struggling and need to talk to someone?
Talk to a trained counselor for free. It's 100% anonymous.
Text FIVEABLE to 741741 to get started.
ยฉ 2021 Fiveable, Inc.