AP Java: Basics

If you are taking this class, you've written programs before in some language like JavaScript or Python. In many ways Java programming is similar, but you will notice immediately that the basic Java "Hello, World!" program looks pretty confusing and complicated. Here's a Java program that prints "Hello, World!". Press the Run button and the program output should appear to the right of the code in what is called the console.

Luckily though, one line of code in this program is much more important and interesting than all the rest. For now we are just interested in

System.out.println("Hello, World!");

We will ignore everything else. This line of code an example of a Java statement. If we think of Java as a language like English, a statement is like a sentence. The same way a period ends an English sentence, a semi-colon marks the end of of a Java statement.

"Hello World!" is an example of a Java String. A String is a collection of letters, digits, punctuation and/or spaces. The beginning and end of the String are marked with double quotes (").

print() vs println()

System.out.println() prints first and then inserts a newline character so that whatever is printed next is printed on the next line. Run the following code and you'll notice top and bottom are printed on separate lines.

Java has another printing function. System.out.print() which does NOT insert a newline. Change the first statement to System.out.print("Top"); Then run the program to make sure that "Bottom" is printed on the same line as "Top".

Now change the second statement to System.out.print("Bottom"); and notice that the output doesn’t change since nothing is printed after "Bottom". (You can also insert a newline character by typing \n)

Comments

Lines that begin with two slashes // are ignored by Java. Multiple lines are ignored if they are between /* and */. Comments have no effect on the execution of the program. They are used to create notes to yourself or other programmers. They make it easier for other programmers (and your future self) to understand what you meant to do. As your teacher, I'll sometimes give you some unfinished code with the instructions for completing it in comments.

Arithmetic operators and parentheses

+ - * / are called arithmetic operators and are used for addition, subtraction, multiplication and division. Parentheses work in Java just like normal arithmetic 1 + 4 * 2 evaluates to 9, (1 + 4) * 2 evaluates to 10.

Literals vs Expressions

Double quotes around text tells Java it is an literal. Java will print a literal exactly as written. Here’s a literal "4/4". If I write the same thing without double quotes it's called a expression. Java evaluates expressions to find a result. Run the following program and you will see that Java prints the literals exactly as they are written (including spaces!), and evaluates the expression first and then prints the result.

Debugging Error Messages

Errors in programs are called “bugs.” The process of fixing program errors is called “debugging.” When you try to run a Java program with a bug you will often get an error message. When you are learning a new programming language, you will make many errors. Errors are ok, just fix them and move on. For example, I wanted the following program to display my first name "Art."

Instead of printing my name, the program shows an error message when it runs. In this case I get a message SyntaxError.java: followed by a number. The number tells me what line the computer was confused about. The message is telling me I forgot to put double quotes around my name like "Art"

Formatting Java Code with Spaces

In Java programs, some spaces are required. For example, you need at least one space between keywords like public, class, static, and void The program below is not legal. Run it and you will get an error message that says so.

Other spaces are optional. Here is another version of the program. It runs, but it is difficult to read the code because it is written on one line with no indentation. We use spaces like indentation to make programs easier to read and debug.

Assignment: Four 4s challenge

Use exactly four 4's to write an expression that evaluates to every integer from 1 to 10, using only the four arithmetic operators + - * / and (). No factorials, square roots, exponents, or arithmetic operations are allowed. The first one has already been done for you. Submit the link to your finished program by choosing Share | Link. Go to Google Classroom and and then choose Add Link to submit the assignment. Don't forget to click the Turn In button. If you have extra time, see if you can get the numbers after 10; 11, 12, 13 and so on.