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".

Escape Characters

Escape characters are special symbols in Java that begin with a backslash \ A good way to remember the difference between a backslash and a forward slash is that a backslash leans backwards ( \ ), while a forward slash leans forward ( / ). The Java escape charater for newline is \n. System.out.print("Top\n"); would have the same effect as System.out.println("Top");. Using the escape character for newline allows you to insert a line break anywhere inside some text. Run the following code and you'll see "Top" and "Bottom" on separate lines.

Other escape charaters are:

Try changing the newline in the program to one of the other escape charaters then run the program and see what is printed.

Comments

Lines that begin with two slashes // are ignored by Java. 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

+ - * / are called arithmetic operators and are used for addition, subtraction, multiplication and division.

Literals vs Expressions

Double quotes around text tells Java it is an literal. Java will print a literal exactly as written. Here’s an 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 made a syntax error. Syntax is the grammar and spelling of a computer language. In this example I forgot the double quotes around my name.

Another type of error is a logic error. This time I misspelled my name. The computer doesn’t know my name, so the program runs incorrectly without an error message.

Sometimes a logic error "crashes" the computer and stops the running program. Logic errors that crash the computer are called Exceptions and produce an error message. Here I made the logic error of dividing by zero.

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 decimals, factorials, square roots, exponents, or other symbols 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.