<< Hide Menu
Avanish Gupta
Avanish Gupta
Welcome to AP Computer Science A, or AP CSA for short! In this guide, you will learn the basics of Java, a programming language that is widely used today.
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.
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.
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:
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:
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.
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.
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!
© 2024 Fiveable Inc. All rights reserved.