Lecture 2: Parts of a program

Objectives

  • Understand basic Java syntax
  • Demonstrate identification and fixing of compilation errors

Our first Java Program

One of the simplest programs we can write in Java, or any programming language, is “Hello World”. We are basically asking the program to print out a greeting; this is a common way of verifying that we installed our program correctly and we’re able to see its output. This is typically the first step in debugging: make sure you can run your code and see the result you expected.

Here is what this program looks like in Java:

public class Hello {

    public static void main(String[] args) {
        // generate some simple output
        System.out.println("Hello, World!");
    }
}

If you run this program (in a terminal), its output will be Hello, World! because this is the string (in green) we specified the program to print.

Let’s try this now in a web tool that allows us to run Java code online.

Here is an even simpler program that adds two numbers together in variables x and y and stores the result in a variable called z:

public class Hello {

    public static void main(String[] args) {
        int x = 3;
        int y = 2;
        int z = x + y;
    }
}

What value will be stored in z?

Why can’t we just use English to tell the computer to do something?

Well, perhaps you already can (hello ChatGPT…).

Just ask it How many letter Rs are in the word strawberry? and then try telling it that it’s wrong. Enjoy.

Edit Aug 2024: they apparently fixed this bug.

I wouldn’t use ChatGPT for anything I don’t already know the answer to.

English is a rich but ambiguous language. This means that a simple statement can take on multiple meanings, and only one is right in certain contexts. For example, if we told someone pick up the bat that flew across the field, and there was both a sick bat (mammal) and a baseball bat (wood) at the edge of a baseball field, which one did we mean? Well, that depends if we are animal control, or a baseball player…but the dangerous worst case scenario is someone picks up the sick bat without gloves and now has rabies.

Even something like stop the car at the stop sign is too complex to tell a car – this request must be broken down into multiple steps that include pressing the brake (with what pressure? for how long?) and defining where to actually stop the car (before the white line? a foot into the intersection?) which requires further mathematical calculations. And is a rolling stop okay, or do you have to actually come to a stop (as the law requires)? Tesla Motors found out the hard way.

Solution: because natural (human) languages are ambiguous and often too high-level to communicate a new problem to a computer, we need to write code in a high level programming language, like Java. It still reads like English (once you get good at it), but what each sentence means is completely unambiguous (otherwise it wouldn’t compile).

But, how does a computer work?

While we can sort of buy how the Hello World program prints out that string, for a more complete picture let’s visualize how the hardware of a computer can actually take a Java program, run it, and send the output to the screen. In this case, let’s explore how a computer program would add together the two variables above and store their sum in z.

Take notes! Make sure, by the end of the lecture, you understand at least the following topics:

  • What are the main hardware components of a computer
  • What a file system is, and how to navigate through its folders
  • How a file is stored in memory
  • At a very high level, explain how the Central Processing Unit (CPU) is used by the computer to execute a computer program that is in memory.

High level vs low level programming languages

As we just saw, a Java program needs to be translated, eventually, into machine-level instructions (of 0s and 1s) in order to be executed on the CPU. This is because on a computer, 0s and 1s can be represented through electrical currents (either on or off), and gates can be used to control how the current flows.

Of course, it would, in theory, be possible to just write our program in binary. But this is obviously bad – our examples would take thousands of machine-level instructions to run. No human wants to write in this language. Instead, it’s much better to write in a very human readable (high-) level language, like Java, and use compilers and/or interpreters to translate that into binary, as we just saw.

Some languages, like python, are directly translated into machine-level instructions via an interpreter. For example, when you type python hello.py in your terminal, it would just run the file directly.

In Java, there are actually two steps to getting code to run, which you have/will see in lab this week. In the first step, we use a compiler, called javac, to translate the Java code into an intermediary, lower-level representation called bytecode. When you do something like javac Hello.java you’ll notice this generates a bytecode file called Hello.class. This is a portable intermediary format that you can look at (though it won’t make much sense unless you look up what the bytes are meant to represent) that is computer architecture agnostic. When you feed this bytecode into the interpreter using the java Hello terminal command, it will convert it into the 0s and 1s that your specific CPU understands. Then, your computer can finally run the program and print out your greeting :-)

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

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.

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.