<< Hide Menu
Kashvi Panjolia
Kashvi Panjolia
This unit makes up 2.5%-5% of the AP Computer Science A exam. While this is not a significant portion of the exam, the topics covered in this unit are ESSENTIAL to succeeding in this course. AP Computer Science A (or AP CSA for short) is a course that builds on previous topics covered, so make sure you know this unit inside and out, and come back to review it frequently. You will use these foundations in every subsequent unit.
The technology you use in your everyday life -- your phone, your computer, the Starbucks app ☕️ -- was made possible through code. Programming is the process of writing that code. With programming, you can build websites, mobile apps, Chrome extensions, and many other types of software. This software can make your life more convenient and can be used to help others as well.
Computers understand commands in sets of 0's and 1's, which is called binary code. However, humans do not understand commands in binary, so programming languages were invented to allow humans to create software. A programming language acts as a translator between human text and binary so humans can give computers instructions to create software. The early programming languages, such as Assembly and BASIC (Beginner's All-purpose Symbolic Instruction Code) are considered low-level programming languages because they were still very difficult to understand by humans and easier to understand by computers.
Over time, humans developed high-level programming languages that were more text-based and easily readable by humans. Examples of high-level programming languages include Python, Kotlin, and -- you guessed it -- Java. Since these languages are further from the binary code computers understand, they need to be compiled. A compiler is a program that translates code written in a high-level programming language into machine code (binary) that can be executed by a computer. Examples of compilers include IntelliJ, Visual Studio Code, and repl.it.
Now that you understand the basics of programming languages, let's get into some code. 🧑💻 This is the first snippet of code you will see when you open up your IDE (Integrated Development Environment).
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Let's break it down. In the first line, you are creating a class called Main that is public, meaning anyone can see it. A class is a template, or blueprint, for creating objects, and it defines methods and variables common to all objects of that class. In the class Main, a method called "main" is defined in line 2. This method is public, static (which you will learn about later in the course), and has a return type of void, telling the computer that nothing is returned. In line 3, the method main is printing out "Hello world!" to the console, the place where you can see what your program is outputting. You need to know the difference between System.out.println(); and System.out.print();. System.out.println(); is a command that inserts a new line BEFORE the content it is printing to the console, while System.out.print(); simply prints the content on an existing line.
In this snippet, "Hello world!" is a string literal because it is enclosed in double quotes. "12345" and "Taco cat" are also string literals. A string literal is an instance of the String class, so you can use all the methods included in the string class, like .length(), on these literals.
A primitive data type is a piece of data that is stored directly in the memory of the computer. In Java, there are eight primitive data types, but for AP CSA, you only need to know three: int, double, and boolean. An int is an integer -- any positive or negative whole number, including 0. A double is decimal (you can remember that double and decimal both start with a "d") and can be used for numbers with up to 15 decimal places. A boolean only stores two values: true or false.
Variables are how Java stores data in the computer so the data can be accessed later. Variables need to be declared and initialized in order to be usable. You can do this in two lines:
int age;
age = 15;
or combine the two statements into one line, which is the preferred way of creating a variable:
int age = 15;
You also need to know about naming conventions. There are special words in Java, called reserved words, that you cannot name your variables, such as "new" or "final." These words are reserved because they are used to perform special functions in Java, and the computer will get confused if you use these words. Another naming convention is using descriptive names for your variables. While it would be fun to name a variable describing the cost of ice cream "foo," a more appropirate name would be costOfIceCream
because it tells you, and anyone else who might be working on the program with you, exactly what the variable is storing.
The word "final" can be used to make a variable constant, meaning it never changes throughout the program (see why "final" is a reserved word?).
If you want the user of your program to interact with it, you will need to ask the user for input. In Java, you can read user input using the Scanner class. Be sure to import this class because it is not included in the standard Java package. Create an instance of the Scanner class like this:
import java.util.Scanner;
Scanner input = new Scanner(System.in);
You can then use this Scanner, called input, to read user input through various methods.
input.nextLine(); reads the next line of text
input.nextInt(); reads the next integer
input.nextDouble(); reads the next double
input.nextBoolean(); reads the next boolean
If you call the method input.nextBoolean()
but the user enters an integer, your program will throw an InputMismatchException. It is important that you learn to understand the errors and exceptions your program throws so you can fix your code quickly. ✓
Java can also be used to perform basic arithmetic. 🔢 Here is a table of the basic arithmetic operators in Java:
+ | This is the addition symbol, used to add two numbers. |
- | This is the subtraction symbol, used to subtract two numbers. |
* | This is the multiplication symbol, used to multiply two numbers. |
/ | This is the division symbol, used to divide two numbers. |
% | This is the modulo operator, used to find the remainder of the division of two numbers. |
The equals sign is called the assignment operator because it assigns the variable a number. If the numbers in an arithmetic operation are all integers, the result will be an integer. If even one of the numbers is a double, the result will be a double.
There are two types of division in Java: integer division and double division. Both types use /
as the division operator, but they differ in their results. Here is an example of integer division:
int quotient = 5 / 2;
You know the answer to this division problem is 2.5, but the computer will store the variable "quotient" as 2. This is because the variable type of the variable quotient is int, so the computer truncates, or cuts off, the value after the decimal. This is called integer division, and it doesn't always result in the most accurate answers.
Double division, however, is much more useful. Here is an example:
double quotient2 = 5 / 2.0;
Now the computer will store the variable quotient2 as 2.5. Notice how the type of the variable is now double, and how 2 was turned into a double by writing it as 2.0. Double division will give you more accurate answers. 👍
Creating variables is great and all, but the real magic happens when you manipulate them. In order to add 1 to a variable, you can write it as age = age + 1;
but this requires a lot of words to just add 1, so you can shorten it to age += 1;
. There is an even shorter way to increment a variable that only works when you want to increase it by 1: age++;
. For increasing a variable by any other number, you can use age += 3;
or another number.
The +=
is an example of a compound operator, which means it does two things in one statement: it adds 3 to age, then assigns the new value to age. Other compound operators in Java include -=
, *=
, /=
, and %=
. Subtracting in Java is much the same as addition. Use age -= 3;
for decreasing a variable by 3, and age--;
for decreasing a variable by 1. The ++
and --
are known as increment and decrement operators, respectively.
Computers cannot store every number that exists. In Java, you can discover the maximum number the computer can store by using Integer.MAX_VALUE, and the minimum number by using Integer.MIN_VALUE.
Sometimes, you need to change the type of data mid-program. Fortunately, Java has an easy solution for this called casting. Casting allows you to change between data types, provided the data types are compatible. Here is an example:
int years = (int) (1.0 + 5.0);
The type the expression should be casted to is int, which is why the int is in parentheses on the right side of the assignment. Notice how the entire expression 1.0 + 5.0 was placed in parentheses. This is because the result of this expression should be casted to an int. If there were no parentheses, the cast would only have applied to the first number, 1.0, leaving the 5.0 as a double, which would cause a compilation error.
© 2024 Fiveable Inc. All rights reserved.