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". (You can also insert a newline character by typing
Lines that begin with two slashes
// are ignored by Java. Multiple lines are ignored
if they are between
*/. 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
and division. Parentheses work in Java just like normal arithmetic
1 + 4 * 2 evaluates to 9,
(1 + 4) * 2 evaluates to 10.
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 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
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 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.