Lecture 2: Parts of a program
Objectives
- Understand basic Java syntax
- Explain the purpose of escape sequences
- Demonstrate identification and fixing of compilation errors
Files vs programs
Keep in mind to always verify that your file has the same name as the class it contains.
Your code is in a file called Hello.java
You are using a program, the Terminal, to access all the files on your computer.
You will use another program, javac
, to compile your file. Then, you’ll use a different program, java
to execute your compiled file (which will be called Hello.class
, as you will see next class).
Now the confusing part: technically, all programs are also files stored somewhere on your system. For example, if you navigate to certain folders (such as Applications or Programs) you can find some of your programs there. The operating system, which you have direct access to in your Terminal program, allows you to run other programs (such as javac
and java
) from the Terminal.
Java syntax basics
public class Hello {
public static void main(String[] args) {
// generate some simple output
System.out.println("Hello, World!");
System.out.println("How are you?");
}
}
Take a look at the program above – it’s Hello World, but we added another print statement.
Let’s examine the different elements in the file above; most of it won’t make sense now, but it will all make sense by the end of the semester!
public class Hello {
...
}
The first and last lines declare a class called Hello
. These light blue words, like Hello
, String
, args
, and System
are called identifiers that, at some point, a human picked, to represent a variable or type.
The curly braces are used to group code into scope blocks (or blocks for now); the class itself is the only thing in this file.
The bolded blue words like public
and class
are reserved words that have special meaning in Java (so they can’t be used by humans as identifiers because that would confuse the compiler). For now, don’t worry about what they mean.
Let’s look at the next line:
public class Hello {
public static void main(String[] args) {
...
}
}
Here we see the reserved words public
again, as well as static
and void
. Unfortunately, different text editors choose different colors for syntax highlighting, even for all reserved words, so you’ll have to learn what are identifers (you choose their names) vs reserved words (Java already has a meaning for them) the hard way :-)
You’ll also notice another identifier, main
, which is the name of a function (technically a method) that contains the entire program we want to run. This main
method is very special in Java – when you run a Java program, the java
interpreter assumes and looks for a main
method in whatever class name you passed to the program on the terminal. So when you do
javac Hello.java
java Hello
that second command is telling Java to open up this class we see above (called Hello
) and find the main
method in it and start executing code. Notice that the main
method is followed by parentheses ()
that contain the arguments to the method.
The last new bit of syntax are the brackets []
which are used to refer to lists (called arrays). Here, they are used for command line arguments (but you can ignore them for now).
What next? Now that we know that whatever is inside main
will get run, let’s look at those three lines – this is what we really care about at this point in the semester:
public class Hello {
public static void main(String[] args) {
// generate some simple output
System.out.println("Hello, World!");
System.out.println("How are you?");
}
}
The first line is a comment in Java, which is just human text the compiler will ignore. You can use //
at the start of a line to generate a comment, or use a comment block with /* */
as you will see soon.
And the last two lines are our two print statements. System
, out
, and println
are all identifiers. System
is a class in Java that represents the operating system; we use the dot operator .
to access one of its components, out
, which refers to a place we can write output to: the terminal. Then, another method called println
is called using the dot operator: it will actually print something to the terminal. You can also see, once again, that both println
methods are using the parentheses to specify arguments to this method (like main
did above).
Next, you can see two strings in green: "Hello, World!"
and "How are you?"
. The double quotes "
around them make them strings; this is a type in Java (technically String
– the same String
you saw in the second line of this example). We’ll introduce them later, but for now you can see how a string can be passed as an argument to the println
method, and it gets printed out :-)
Note that these two statements that actually do something (in this case, print) are terminated with a semicolon ;
in Java.
Finally, everything in Java is case sensitive. Everything. Really, everything. When things aren’t compiling, check your cases :-)
Strings and escape sequences
In Java we use double quotes to indicate a string. For example,
System.out.println("System");
Here System
is an identifier that refers to a class in Java that gives you access to the operating system, while "System"
is just a string. The line above would print the string System
to the terminal.
Sometimes, we want to put double quotes in strings. This is a problem, since the double quote is used as the delimiter:
System.out.println("She said "How are you?", and smiled.");
You can see the code above looks broken.
You could get around this by replacing the double quote with a single quote:
System.out.println("She said 'How are you?', and smiled.");
But sometimes we don’t want to have to do that. To allow us to store double quotes (and other special characters) inside of string, we use the backslash \
as an escape character that tell the computer to ignore the upcoming character. For example, we could solve our issue above with:
System.out.println("She said \"How are you?\", and smiled.");
There are many other escape sequences available in Java, where the \
is combined with the character after it to represent something unsual:
\t
insert a tab at this point\n
insert a new line at this point\"
: insert a (literal) double quotes at this point (we just saw this)\\
: insert a (literal) backslash at this point
Syntax vs semantics
We’ve covered a lot of syntax today, but the vast majority of this class will not be about Java syntax. This is not a course on Java; it is a course on problem decomposition and algorithm design: we will only introduce and worry about the minimal syntax we need this semester to write our basic programs. Keep in mind the difference below:
Syntax: The set of rules that define well formed sentences in a given language. In other words: Is the spelling correct, given the language (Java)? Example: the sentence He!lp the d,og ate the pizza
has incorrect syntax (even if, as a human, you might be able to pick up on the meaning).
Semantics: This looks at the meaning of the sentences rather than at how they are written. In other words, does the code actually do what we want it to do?. Example: the sentence Help, the pizza ate the dog!
has correct syntax but it does not reflect what the person attempted to say.
Next class
We’ll discuss variables and numeric types; this means we’ll be able to do basic math.
Prep for next time
Study for the 3-minute open-note BB quiz we’ll have at 11:10am the next class on the upcoming Lecture 03 notes.