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
"Hello World!" is an example of a Java
String is a collection of letters, digits,
punctuation and/or spaces. The beginning and end of the String are marked with double quotes (
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 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
System.out.print("Top\n"); would have the same effect as
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:
\tInsert a tab in the text at this point
\'Insert a single quote in the text at this point
\"Insert a double quote in the text at this point
\\Insert a backslash in the text at this point
Try changing the newline in the program to one of the other escape charaters then run the program and see what is printed.
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.
+ - * / are called arithmetic operators and are used for addition, subtraction, multiplication
Double quotes around text tells Java it is an literal. Java will print a literal exactly as written. Here’s
"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.
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.
In Java programs, some spaces are required. For example, you need at least one space between keywords like
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.
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.